diff --git a/lib/Teapot/Bot/Brain.pm b/lib/Teapot/Bot/Brain.pm index 453605d..effa90e 100644 --- a/lib/Teapot/Bot/Brain.pm +++ b/lib/Teapot/Bot/Brain.pm @@ -276,20 +276,15 @@ sub deleteMessage { my $token = $self->token || croak 'No token supplied to deleteMessage()?'; - if (Teapot::Bot::Object::ChatMember::canDelete($self, $chatid)) { + if (Teapot::Bot::Object::ChatMember::canDeleteMessage($self, $args->{chat_id})) { my $url = "https://api.telegram.org/bot${token}/deleteMessage"; - my $api_response = $self->_post_request($url, $send_args); - - if ($api_response) { - return Teapot::Bot::Object::Message->create_from_hash($api_response, $self); - } - else { - return {'error' => 1}; - } + $self->_post_request($url, $send_args); } else { return {'error' => 1}; } + + return; } diff --git a/lib/Teapot/Bot/Object/ChatMember.pm b/lib/Teapot/Bot/Object/ChatMember.pm index 264d392..5ea57e8 100644 --- a/lib/Teapot/Bot/Object/ChatMember.pm +++ b/lib/Teapot/Bot/Object/ChatMember.pm @@ -48,7 +48,7 @@ sub canDeleteMessage { if ($chatid < 0) { # group chat - my $chatobj = $self->_brain->getChat ({ 'chat_id' => $chatid }); + my $chatobj = $self->getChat ({ 'chat_id' => $chatid }); # on api error, keep silence unless ($chatobj) { @@ -56,7 +56,7 @@ sub canDeleteMessage { return 0; } - my $myObj = $self->_brain->getMe (); + my $myObj = $self->getMe (); # on api error, keep silence unless ($chatobj) { @@ -65,7 +65,7 @@ sub canDeleteMessage { } my $myid = $myObj->id; - my $me = $self->_brain->getChatMember ({ 'chat_id' => $chatid, 'user_id' => $myid }); + my $me = $self->getChatMember ({ 'chat_id' => $chatid, 'user_id' => $myid }); # on api error, keep silence unless ($me) { @@ -78,8 +78,8 @@ sub canDeleteMessage { if ($me->{'status'} eq 'administrator') { $can_delete = 1; } - # in supergroup bot must be granted with - } elsif ($chatobj->{'type'} eq 'group') || ($chatobj->{'type'} eq 'channel') + # in supergroup bot must be granted with can_delete_messages + } elsif (($chatobj->{'type'} eq 'supergroup') || ($chatobj->{'type'} eq 'channel')) { if ($me->{'can_delete_messages'}) { $can_delete = 1; } diff --git a/lib/admin.pm b/lib/admin.pm index 4d8274b..ccb735e 100644 --- a/lib/admin.pm +++ b/lib/admin.pm @@ -14,12 +14,13 @@ use util qw (utf2sha1); use version; our $VERSION = qw (1.0); use Exporter qw (import); +# to export array we need @ISA here our @ISA = qw / Exporter /; ## no critic (ClassHierarchies::ProhibitExplicitISA) our @EXPORT_OK = qw (@forbiddenMessageTypes getForbiddenTypes addForbiddenType delForbiddenType listForbidden fortune_toggle fortune_toggle_list fortune_status); my $c = loadConf (); my $dir = $c->{admin}->{dir}; -my @forbiddenMessageTypes = qw (audio voice photo video video_note animation sticker dice game poll document); +our @forbiddenMessageTypes = qw (audio voice photo video video_note animation sticker dice game poll document); ## no critic (Variables::ProhibitPackageVars) sub initCensorDB { my $backingfile = shift; @@ -27,7 +28,7 @@ sub initCensorDB { tie my %censor, 'SQLite_File', $backingfile || cluck "Unable to tie to $backingfile: $OS_ERROR"; foreach (@forbiddenMessageTypes) { - $censor($_) = 0; + $censor{$_} = 0; } untie %censor; @@ -38,16 +39,24 @@ sub initCensorDB { sub getForbiddenTypes { my $chatid = shift; + unless (-d $dir) { + unless (make_path $dir) { + cluck "Unable to create $dir: $OS_ERROR"; + return; + } + } + my $messageTypes; my $backingfile = utf2sha1 $chatid; $backingfile =~ s/\//-/xmsg; $backingfile = sprintf '%s/censor_%s.db', $dir, $backingfile; + my %censor; unless (-f $backingfile) { initCensorDB ($backingfile); } - unless (tie my %censor, 'SQLite_File', $backingfile) { + unless (tie %censor, 'SQLite_File', $backingfile) { cluck "Unable to tie to $backingfile: $OS_ERROR"; return $messageTypes; } @@ -83,7 +92,9 @@ sub addForbiddenType { initCensorDB ($backingfile); } - unless (tie my %censor, 'SQLite_File', $backingfile) { + my %censor; + + unless (tie %censor, 'SQLite_File', $backingfile) { cluck "Unable to tie to $backingfile: $OS_ERROR"; return; } @@ -114,7 +125,9 @@ sub delForbiddenType { return; } - unless (tie my %censor, 'SQLite_File', $backingfile) { + my %censor; + + unless (tie %censor, 'SQLite_File', $backingfile) { cluck "Unable to tie to $backingfile: $OS_ERROR"; return; } @@ -128,6 +141,13 @@ sub delForbiddenType { sub listForbidden { my $chatid = shift; + unless (-d $dir) { + unless (make_path $dir) { + cluck "Unable to create $dir: $OS_ERROR"; + return; + } + } + my @list; my $backingfile = utf2sha1 $chatid; $backingfile =~ s/\//-/xmsg; @@ -137,8 +157,10 @@ sub listForbidden { initCensorDB ($backingfile); } + my %censor; + # FIXME: return list with all types alowed - unless (tie my %censor, 'SQLite_File', $backingfile) { + unless (tie %censor, 'SQLite_File', $backingfile) { cluck "Unable to tie to $backingfile: $OS_ERROR"; return ''; } diff --git a/lib/botlib.pm b/lib/botlib.pm index 7296102..2d7799a 100644 --- a/lib/botlib.pm +++ b/lib/botlib.pm @@ -53,7 +53,7 @@ sub command { my $text = shift; my $chatid = shift; my $reply; - my ($userid, $username, $fullname, $highlight, $visavi) = highlight $msg; + my ($userid, $username, $fullname, $highlight, $visavi) = highlight ($msg); if (substr ($text, 1) eq 'ping') { $reply = 'Pong.'; @@ -130,10 +130,10 @@ MYHELP $msg->replyMd ($reply); return; } elsif (substr ($text, 1) eq 'admin' || substr ($text, 1) eq 'админ') { - my $member = $self->getChatMember ($msg->from->id); + my $member = $self->getChatMember ({ 'chat_id' => $msg->chat->id, 'user_id' => $msg->from->id }); # this msg should be shown only if admins of chat request it - if ($member->status eq 'administrator') || ($member->status eq 'creator') { + if (($member->status eq 'administrator') || ($member->status eq 'creator')) { $reply = << "MYADMIN"; ``` ${csign}admin censor type # - где 1 - вкл, 0 - выкл цензуры для означенного типа сообщений @@ -146,25 +146,26 @@ ${csign}admin fortune - показываем ли с утра фортун ${csign}admin фортунка - показываем ли с утра фортунку для чата ``` Типы сообщений: -audio voice photo video video_note animation sticker dice game poll document +audio voice photo video animation sticker dice game poll document MYADMIN + $msg->replyMd ($reply); } return; - } elsif ((substr ($text, 1, 5) eq 'admin' || substr ($text, 1, 5) eq 'админ') and (length ($text >= 8))) { - my $member = $self->getChatMember ($msg->from->id); + } elsif ((substr ($text, 1, 5) eq 'admin' || substr ($text, 1, 5) eq 'админ') && (length ($text) >= 8)) { + my $member = $self->getChatMember ({ 'chat_id' => $msg->chat->id, 'user_id' => $msg->from->id }); # this msg should be shown only if admins of chat request it - if ($member->status eq 'administrator') || ($member->status eq 'creator') { + if (($member->status eq 'administrator') || ($member->status eq 'creator')) { my $command = trim (substr $text, 7); my ($cmd, $args) = split /\s+/, $command, 2; if ($cmd eq '') { return; } elsif ($cmd eq 'censor' || $cmd eq 'ценз') { - if (defined $args && args ne '') { - my ($msgType, $toggle) = split /\s/, $args; + if (defined ($args) && ($args ne '')) { + my ($msgType, $toggle) = split /\s/, $args, 2; if (defined $toggle) { foreach (@forbiddenMessageTypes) { @@ -260,9 +261,9 @@ sub botsleep { sub isCensored { my $msg = shift; - $forbidden = getForbiddenTypes ($msg->{chat}->{id}); + my $forbidden = getForbiddenTypes ($msg->chat->id); - foreach (keys %{%forbidden}) { + foreach (keys %{$forbidden}) { if ($forbidden->{$_} && $msg->can ($_) && (defined $msg->{$_})) { return 1; } diff --git a/lib/telegrambot.pm b/lib/telegrambot.pm index 3c54051..0ddfd9c 100644 --- a/lib/telegrambot.pm +++ b/lib/telegrambot.pm @@ -221,7 +221,7 @@ sub __on_msg { my $reply; if (isCensored $msg) { - carp '[INFO] In public chat %s (%s) message from %s was censored', $chatname, $chatid, $vis_a_vi; + carp sprintf '[INFO] In public chat %s (%s) message from %s was censored', $chatname, $chatid, $vis_a_vi; $self->deleteMessage ({chat_id => $chatid, message_id => $msg->{message_id}}); }