move admin to CHI
This commit is contained in:
298
lib/admin.pm
298
lib/admin.pm
@@ -7,8 +7,8 @@ use utf8;
|
|||||||
use open qw (:std :utf8);
|
use open qw (:std :utf8);
|
||||||
use English qw ( -no_match_vars );
|
use English qw ( -no_match_vars );
|
||||||
use Carp qw (cluck croak);
|
use Carp qw (cluck croak);
|
||||||
use File::Path qw (make_path);
|
use CHI;
|
||||||
use SQLite_File;
|
use CHI::Driver::BerkeleyDB;
|
||||||
use conf qw (loadConf);
|
use conf qw (loadConf);
|
||||||
use util qw (utf2sha1);
|
use util qw (utf2sha1);
|
||||||
|
|
||||||
@@ -19,25 +19,11 @@ our @ISA = qw / Exporter /; ## no critic (ClassHierarchies::ProhibitExplicitI
|
|||||||
our @EXPORT_OK = qw (@forbiddenMessageTypes @pluginList getForbiddenTypes addForbiddenType delForbiddenType listForbidden fortune_toggle fortune_toggle_list fortune_status plugin_toggle plugin_status pluginEnabled);
|
our @EXPORT_OK = qw (@forbiddenMessageTypes @pluginList getForbiddenTypes addForbiddenType delForbiddenType listForbidden fortune_toggle fortune_toggle_list fortune_status plugin_toggle plugin_status pluginEnabled);
|
||||||
|
|
||||||
my $c = loadConf ();
|
my $c = loadConf ();
|
||||||
my $dir = $c->{admin}->{dir};
|
my $cachedir = $c->{cachedir};
|
||||||
# this list is not yet used
|
# this list is not yet used
|
||||||
our @pluginList = qw (oboobs obutts); ## no critic (Variables::ProhibitPackageVars)
|
our @pluginList = qw (oboobs obutts); ## no critic (Variables::ProhibitPackageVars)
|
||||||
our @forbiddenMessageTypes = qw (audio voice photo video video_note animation sticker dice game poll document); ## no critic (Variables::ProhibitPackageVars)
|
our @forbiddenMessageTypes = qw (audio voice photo video video_note animation sticker dice game poll document); ## no critic (Variables::ProhibitPackageVars)
|
||||||
|
|
||||||
sub initCensorDB {
|
|
||||||
my $backingfile = shift;
|
|
||||||
|
|
||||||
tie my %censor, 'SQLite_File', $backingfile || cluck "Unable to tie to $backingfile: $OS_ERROR";
|
|
||||||
|
|
||||||
foreach (@forbiddenMessageTypes) {
|
|
||||||
$censor{$_} = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
untie %censor;
|
|
||||||
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
sub getForbiddenTypes {
|
sub getForbiddenTypes {
|
||||||
my $chatid = shift;
|
my $chatid = shift;
|
||||||
|
|
||||||
@@ -49,29 +35,28 @@ sub getForbiddenTypes {
|
|||||||
}
|
}
|
||||||
|
|
||||||
my $messageTypes;
|
my $messageTypes;
|
||||||
my $backingfile = utf2sha1 $chatid;
|
|
||||||
$backingfile =~ s/\//-/xmsg;
|
|
||||||
$backingfile = sprintf '%s/censor_%s.db', $dir, $backingfile;
|
|
||||||
my %censor;
|
|
||||||
|
|
||||||
unless (-f $backingfile) {
|
my $cache = CHI->new (
|
||||||
initCensorDB ($backingfile);
|
driver => 'BerkeleyDB',
|
||||||
}
|
root_dir => $cachedir,
|
||||||
|
namespace => __PACKAGE__ . '_' . 'censor' . '_' . utf2sha1 ($chatid)
|
||||||
|
);
|
||||||
|
|
||||||
unless (tie %censor, 'SQLite_File', $backingfile) {
|
foreach (@forbiddenMessageTypes) {
|
||||||
cluck "Unable to tie to $backingfile: $OS_ERROR";
|
my $type = $c->get ($_);
|
||||||
return $messageTypes;
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach (keys %censor) {
|
if ($type) {
|
||||||
if ($censor{$_}) {
|
$messageTypes->{$_} = 1;
|
||||||
|
} else {
|
||||||
|
unless (defined $type) {
|
||||||
|
$c->set ($_, 1, 'never');
|
||||||
$messageTypes->{$_} = 1;
|
$messageTypes->{$_} = 1;
|
||||||
} else {
|
} else {
|
||||||
$messageTypes->{$_} = 0;
|
$messageTypes->{$_} = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
untie %censor;
|
|
||||||
return $messageTypes;
|
return $messageTypes;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -79,31 +64,13 @@ sub addForbiddenType {
|
|||||||
my $chatid = shift;
|
my $chatid = shift;
|
||||||
my $type = shift;
|
my $type = shift;
|
||||||
|
|
||||||
unless (-d $dir) {
|
my $cache = CHI->new (
|
||||||
unless (make_path $dir) {
|
driver => 'BerkeleyDB',
|
||||||
cluck "Unable to create $dir: $OS_ERROR";
|
root_dir => $cachedir,
|
||||||
return;
|
namespace => __PACKAGE__ . '_' . 'censor' . '_' . utf2sha1 ($chatid)
|
||||||
}
|
);
|
||||||
}
|
|
||||||
|
|
||||||
my $backingfile = utf2sha1 $chatid;
|
|
||||||
$backingfile =~ s/\//-/xmsg;
|
|
||||||
$backingfile = sprintf '%s/censor_%s.db', $dir, $backingfile;
|
|
||||||
|
|
||||||
unless (-f $backingfile) {
|
|
||||||
initCensorDB ($backingfile);
|
|
||||||
}
|
|
||||||
|
|
||||||
my %censor;
|
|
||||||
|
|
||||||
unless (tie %censor, 'SQLite_File', $backingfile) {
|
|
||||||
cluck "Unable to tie to $backingfile: $OS_ERROR";
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
$censor{$type} = 1;
|
|
||||||
untie %censor;
|
|
||||||
|
|
||||||
|
$cache->set ($type, 1, 'never');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -111,71 +78,40 @@ sub delForbiddenType {
|
|||||||
my $chatid = shift;
|
my $chatid = shift;
|
||||||
my $type = shift;
|
my $type = shift;
|
||||||
|
|
||||||
unless (-d $dir) {
|
my $cache = CHI->new (
|
||||||
unless (make_path $dir) {
|
driver => 'BerkeleyDB',
|
||||||
cluck "Unable to create $dir: $OS_ERROR";
|
root_dir => $cachedir,
|
||||||
return;
|
namespace => __PACKAGE__ . '_' . 'censor' . '_' . utf2sha1 ($chatid)
|
||||||
}
|
);
|
||||||
}
|
|
||||||
|
|
||||||
my $backingfile = utf2sha1 $chatid;
|
|
||||||
$backingfile =~ s/\//-/xmsg;
|
|
||||||
$backingfile = sprintf '%s/censor_%s.db', $dir, $backingfile;
|
|
||||||
|
|
||||||
unless (-f $backingfile) {
|
|
||||||
initCensorDB ($backingfile);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
my %censor;
|
|
||||||
|
|
||||||
unless (tie %censor, 'SQLite_File', $backingfile) {
|
|
||||||
cluck "Unable to tie to $backingfile: $OS_ERROR";
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
$censor{$type} = 0;
|
|
||||||
untie %censor;
|
|
||||||
|
|
||||||
|
$cache->remove ($type);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
sub listForbidden {
|
sub listForbidden {
|
||||||
my $chatid = shift;
|
my $chatid = shift;
|
||||||
|
|
||||||
unless (-d $dir) {
|
|
||||||
unless (make_path $dir) {
|
|
||||||
cluck "Unable to create $dir: $OS_ERROR";
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
my @list;
|
my @list;
|
||||||
my $backingfile = utf2sha1 $chatid;
|
|
||||||
$backingfile =~ s/\//-/xmsg;
|
|
||||||
$backingfile = sprintf '%s/censor_%s.db', $dir, $backingfile;
|
|
||||||
|
|
||||||
unless (-f $backingfile) {
|
my $cache = CHI->new (
|
||||||
initCensorDB ($backingfile);
|
driver => 'BerkeleyDB',
|
||||||
}
|
root_dir => $cachedir,
|
||||||
|
namespace => __PACKAGE__ . '_' . 'censor' . '_' . utf2sha1 ($chatid)
|
||||||
|
);
|
||||||
|
|
||||||
my %censor;
|
foreach (@forbiddenMessageTypes) {
|
||||||
|
my $type = $c->get ($_);
|
||||||
|
|
||||||
# FIXME: return list with all types alowed
|
if ($type) {
|
||||||
unless (tie %censor, 'SQLite_File', $backingfile) {
|
|
||||||
cluck "Unable to tie to $backingfile: $OS_ERROR";
|
|
||||||
return '';
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach (keys %censor) {
|
|
||||||
if ($censor{$_}) {
|
|
||||||
push @list, sprintf 'Тип сообщения %s удаляется', $_;
|
push @list, sprintf 'Тип сообщения %s удаляется', $_;
|
||||||
|
} else {
|
||||||
|
unless (defined $type) {
|
||||||
|
push @list, sprintf 'Тип сообщения %s удаляется', $_;
|
||||||
|
$c->set ($_, 1, 'never');
|
||||||
} else {
|
} else {
|
||||||
push @list, sprintf 'Тип сообщения %s не удаляется', $_;
|
push @list, sprintf 'Тип сообщения %s не удаляется', $_;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
untie %censor;
|
|
||||||
|
|
||||||
return join "\n", @list;
|
return join "\n", @list;
|
||||||
}
|
}
|
||||||
@@ -185,44 +121,35 @@ sub fortune_toggle (@) {
|
|||||||
my $action = shift // undef;
|
my $action = shift // undef;
|
||||||
my $phrase = 'Фортунка с утра ';
|
my $phrase = 'Фортунка с утра ';
|
||||||
|
|
||||||
unless (-d $dir) {
|
my $cache = CHI->new (
|
||||||
make_path ($dir) || do {
|
driver => 'BerkeleyDB',
|
||||||
cluck "[ERROR] Unable to create $dir: $OS_ERROR";
|
root_dir => $cachedir,
|
||||||
return;
|
namespace => __PACKAGE__ . '_' . 'fortune'
|
||||||
};
|
);
|
||||||
}
|
|
||||||
|
|
||||||
my $backingfile = sprintf '%s/fortune_morning.sqlite', $dir;
|
my $state = $cache->get ($chatid);
|
||||||
|
|
||||||
tie my %toggle, 'SQLite_File', $backingfile || do {
|
|
||||||
cluck "[ERROR] Unable to tie to $backingfile: $OS_ERROR";
|
|
||||||
return;
|
|
||||||
};
|
|
||||||
|
|
||||||
my $state = $toggle{$chatid};
|
|
||||||
|
|
||||||
if (defined $action) {
|
if (defined $action) {
|
||||||
if ($action) {
|
if ($action) {
|
||||||
$toggle{$chatid} = 1;
|
$cache->set ($chatid, 1, 'never');
|
||||||
$phrase .= 'будет показываться.';
|
$phrase .= 'будет показываться.';
|
||||||
} else {
|
} else {
|
||||||
if (defined $state) {
|
if (defined $state) {
|
||||||
delete $toggle{$chatid};
|
$cache->remove ($chatid);
|
||||||
}
|
}
|
||||||
|
|
||||||
$phrase .= 'не будет показываться.';
|
$phrase .= 'не будет показываться.';
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (defined $state && $state) {
|
if (defined $state && $state) {
|
||||||
delete $toggle{$chatid};
|
$cache->remove ($chatid);
|
||||||
$phrase .= 'не будет показываться.';
|
$phrase .= 'не будет показываться.';
|
||||||
} else {
|
} else {
|
||||||
$toggle{$chatid} = 1;
|
$cache->set ($chatid, 1, 'never');
|
||||||
$phrase .= 'будет показываться.';
|
$phrase .= 'будет показываться.';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
untie %toggle;
|
|
||||||
return $phrase;
|
return $phrase;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -230,21 +157,13 @@ sub fortune_status ($) {
|
|||||||
my $chatid = shift;
|
my $chatid = shift;
|
||||||
my $phrase = 'Фортунка с утра ';
|
my $phrase = 'Фортунка с утра ';
|
||||||
|
|
||||||
unless (-d $dir) {
|
my $cache = CHI->new (
|
||||||
make_path ($dir) || do {
|
driver => 'BerkeleyDB',
|
||||||
cluck "[ERROR] Unable to create $dir: $OS_ERROR";
|
root_dir => $cachedir,
|
||||||
return;
|
namespace => __PACKAGE__ . '_' . 'fortune'
|
||||||
};
|
);
|
||||||
}
|
|
||||||
|
|
||||||
my $backingfile = sprintf '%s/fortune_morning.sqlite', $dir;
|
my $state = $cache->get ($chatid);
|
||||||
|
|
||||||
tie my %toggle, 'SQLite_File', $backingfile || do {
|
|
||||||
cluck "[ERROR] Unable to tie to $backingfile: $OS_ERROR\n";
|
|
||||||
return;
|
|
||||||
};
|
|
||||||
|
|
||||||
my $state = $toggle{$chatid};
|
|
||||||
|
|
||||||
if (defined $state && $state) {
|
if (defined $state && $state) {
|
||||||
$phrase .= 'показывается.';
|
$phrase .= 'показывается.';
|
||||||
@@ -252,29 +171,17 @@ sub fortune_status ($) {
|
|||||||
$phrase .= 'не показывается.';
|
$phrase .= 'не показывается.';
|
||||||
}
|
}
|
||||||
|
|
||||||
untie %toggle;
|
|
||||||
return $phrase;
|
return $phrase;
|
||||||
}
|
}
|
||||||
|
|
||||||
sub fortune_toggle_list () {
|
sub fortune_toggle_list () {
|
||||||
unless (-d $dir) {
|
my $cache = CHI->new (
|
||||||
make_path ($dir) || do {
|
driver => 'BerkeleyDB',
|
||||||
cluck "[ERROR] Unable to create $dir: $OS_ERROR";
|
root_dir => $cachedir,
|
||||||
my @empty = ();
|
namespace => __PACKAGE__ . '_' . 'fortune'
|
||||||
return @empty;
|
);
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
my $backingfile = sprintf '%s/fortune_morning.sqlite', $dir;
|
my @list = $cache->get_keys();
|
||||||
|
|
||||||
tie my %toggle, 'SQLite_File', $backingfile || do {
|
|
||||||
cluck "[ERROR] Unable to tie to $backingfile: $OS_ERROR";
|
|
||||||
my @empty = ();
|
|
||||||
return @empty;
|
|
||||||
};
|
|
||||||
|
|
||||||
my @list = keys %toggle;
|
|
||||||
untie %toggle;
|
|
||||||
return @list;
|
return @list;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -283,24 +190,13 @@ sub plugin_status (@) {
|
|||||||
my $plugin = shift;
|
my $plugin = shift;
|
||||||
my $phrase = "Плагин $plugin ";
|
my $phrase = "Плагин $plugin ";
|
||||||
|
|
||||||
unless (-d $dir) {
|
my $cache = CHI->new (
|
||||||
make_path ($dir) || do {
|
driver => 'BerkeleyDB',
|
||||||
cluck "[ERROR] Unable to create $dir: $OS_ERROR";
|
root_dir => $cachedir,
|
||||||
return;
|
namespace => __PACKAGE__ . '_' . 'plugin' . '_' . utf2sha1 ($chatid)
|
||||||
};
|
);
|
||||||
}
|
|
||||||
|
|
||||||
my $backingfile = utf2sha1 $chatid;
|
my $state = $cache->get ($plugin);
|
||||||
$backingfile =~ s/\//-/xmsg;
|
|
||||||
$backingfile = sprintf '%s/%s.sqlite', $dir, $backingfile;
|
|
||||||
|
|
||||||
tie my %toggle, 'SQLite_File', $backingfile || do {
|
|
||||||
cluck "[ERROR] Unable to tie to $backingfile: $OS_ERROR\n";
|
|
||||||
return;
|
|
||||||
};
|
|
||||||
|
|
||||||
my $state = $toggle{$plugin};
|
|
||||||
untie %toggle;
|
|
||||||
|
|
||||||
if (defined $state && $state) {
|
if (defined $state && $state) {
|
||||||
$phrase .= 'включён.';
|
$phrase .= 'включён.';
|
||||||
@@ -315,24 +211,13 @@ sub pluginEnabled (@) {
|
|||||||
my $chatid = shift;
|
my $chatid = shift;
|
||||||
my $plugin = shift;
|
my $plugin = shift;
|
||||||
|
|
||||||
unless (-d $dir) {
|
my $cache = CHI->new (
|
||||||
make_path ($dir) || do {
|
driver => 'BerkeleyDB',
|
||||||
cluck "[ERROR] Unable to create $dir: $OS_ERROR";
|
root_dir => $cachedir,
|
||||||
return;
|
namespace => __PACKAGE__ . '_' . 'plugin' . '_' . utf2sha1 ($chatid)
|
||||||
};
|
);
|
||||||
}
|
|
||||||
|
|
||||||
my $backingfile = utf2sha1 $chatid;
|
my $state = $cache->get ($plugin);
|
||||||
$backingfile =~ s/\//-/xmsg;
|
|
||||||
$backingfile = sprintf '%s/%s.sqlite', $dir, $backingfile;
|
|
||||||
|
|
||||||
tie my %toggle, 'SQLite_File', $backingfile || do {
|
|
||||||
cluck "[ERROR] Unable to tie to $backingfile: $OS_ERROR\n";
|
|
||||||
return;
|
|
||||||
};
|
|
||||||
|
|
||||||
my $state = $toggle{$plugin};
|
|
||||||
untie %toggle;
|
|
||||||
|
|
||||||
if (defined $state && $state) {
|
if (defined $state && $state) {
|
||||||
return 1;
|
return 1;
|
||||||
@@ -347,43 +232,32 @@ sub plugin_toggle (@) {
|
|||||||
my $action = shift // undef;
|
my $action = shift // undef;
|
||||||
my $phrase = "Плагин $plugin ";
|
my $phrase = "Плагин $plugin ";
|
||||||
|
|
||||||
unless (-d $dir) {
|
my $cache = CHI->new (
|
||||||
make_path ($dir) || do {
|
driver => 'BerkeleyDB',
|
||||||
cluck "[ERROR] Unable to create $dir: $OS_ERROR";
|
root_dir => $cachedir,
|
||||||
my @empty = ();
|
namespace => __PACKAGE__ . '_' . 'plugin' . '_' . utf2sha1 ($chatid)
|
||||||
return @empty;
|
);
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
my $backingfile = utf2sha1 $chatid;
|
my $state = $cache->get ($plugin);
|
||||||
$backingfile =~ s/\//-/xmsg;
|
|
||||||
$backingfile = sprintf '%s/%s.sqlite', $dir, $backingfile;
|
|
||||||
|
|
||||||
tie my %toggle, 'SQLite_File', $backingfile || do {
|
|
||||||
cluck "[ERROR] Unable to tie to $backingfile: $OS_ERROR";
|
|
||||||
my @empty = ();
|
|
||||||
return @empty;
|
|
||||||
};
|
|
||||||
|
|
||||||
if (defined $action) {
|
if (defined $action) {
|
||||||
if ($action) {
|
if ($action) {
|
||||||
$toggle{$plugin} = 1;
|
$cache->set ($plugin, 1, 'never');
|
||||||
$phrase .= 'включён';
|
$phrase .= 'включён';
|
||||||
} else {
|
} else {
|
||||||
$toggle{$plugin} = 0;
|
$cache->set ($plugin, 0, 'never');
|
||||||
$phrase .= 'выключен';
|
$phrase .= 'выключен';
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (defined ($toggle{$plugin}) && $toggle{$plugin}) {
|
if (defined ($state) && $state) {
|
||||||
$toggle{$plugin} = 0;
|
$cache->set ($plugin, 0, 'never');
|
||||||
$phrase .= 'включён';
|
$phrase .= 'включён';
|
||||||
} else {
|
} else {
|
||||||
$toggle{$plugin} = 1;
|
$cache->set ($plugin, 1, 'never');
|
||||||
$phrase .= 'выключен';
|
$phrase .= 'выключен';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
untie %toggle;
|
|
||||||
return $phrase;
|
return $phrase;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user