File Coverage

blib/lib/Bot/ChatBots/Auth.pm
Criterion Covered Total %
statement 40 40 100.0
branch 10 12 83.3
condition 12 15 80.0
subroutine 5 5 100.0
pod 1 1 100.0
total 68 73 93.1


line stmt bran cond sub pod time code
1             package Bot::ChatBots::Auth;
2 2     2   58431 use strict;
  2         9  
  2         44  
3 2     2   7 use warnings;
  2         5  
  2         82  
4             { our $VERSION = '0.014'; }
5              
6 2     2   778 use Log::Any qw< $log >;
  2         14521  
  2         8  
7              
8 2     2   4177 use Moo;
  2         17176  
  2         8  
9             with 'Bot::ChatBots::Role::Processor';
10              
11             has channels => (is => 'rw', default => sub { return {} });
12             has name => (is => 'ro', default => sub { return ref($_[0]) || $_[0] });
13             has users => (is => 'rw', default => sub { return {} });
14              
15             sub process {
16 9     9 1 6992 my ($self, $record) = @_;
17 9         28 my $name = $self->name;
18              
19 9         18 my $users = $self->users;
20 9 50       22 if (keys %$users) {
21 9   66     22 my $id = $record->{sender}{id} // do {
22 2         9 $log->info("$name: sender id is not present");
23 2         9 return;
24             };
25 7 100       13 if (exists $users->{blacklist}{$id}) {
26 1         6 $log->info("$name: sender '$id' is blacklisted, blocking");
27 1         5 return;
28             }
29 6 100 100     9 if (scalar(keys %{$users->{whitelist}})
  6         20  
30             && (!exists($users->{whitelist}{$id})))
31             {
32 1         5 $log->info("$name: sender '$id' not whitelisted, blocking");
33 1         6 return;
34             } ## end if (scalar(keys %{$users...}))
35             } ## end if (keys %$users)
36              
37 5         11 my $channels = $self->channels;
38 5 50       16 if (keys %$channels) {
39 5   66     21 my $id = $record->{channel}{fqid} // $record->{channel}{id} // do {
      66        
40 1         5 $log->info("$name: chat id is not present");
41 1         4 return;
42             };
43 4 100       9 if (exists $channels->{blacklist}{$id}) {
44 1         6 $log->info("$name: chat '$id' is blacklisted, blocking");
45 1         4 return;
46             }
47 3 100 100     4 if (scalar(keys %{$channels->{whitelist}})
  3         11  
48             && (!exists($channels->{whitelist}{$id})))
49             {
50 1         6 $log->info("$name: chat '$id' not whitelisted, blocking");
51 1         4 return;
52             } ## end if (scalar(keys %{$channels...}))
53             } ## end if (keys %$channels)
54              
55 2         9 $log->info("$name: no reason to block, allowing");
56 2         8 return $record;
57             } ## end sub process
58              
59             42;