File Coverage

blib/lib/Bot/ChatBots/Auth.pm
Criterion Covered Total %
statement 40 40 100.0
branch 10 12 83.3
condition 11 15 73.3
subroutine 5 5 100.0
pod 1 1 100.0
total 67 73 91.7


line stmt bran cond sub pod time code
1             package Bot::ChatBots::Auth;
2 2     2   27320 use strict;
  2         4  
  2         68  
3 2     2   11 use warnings;
  2         1  
  2         112  
4             { our $VERSION = '0.006'; }
5              
6 2     2   1253 use Log::Any qw< $log >;
  2         21270  
  2         14  
7              
8 2     2   6598 use Moo;
  2         26027  
  2         17  
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 6786 my ($self, $record) = @_;
17 9         36 my $name = $self->name;
18              
19 9         16 my $users = $self->users;
20 9 50       28 if (keys %$users) {
21 9   66     28 my $id = $record->{sender}{id} // do {
22 2         12 $log->info("$name: sender id is not present");
23 2         10 return;
24             };
25 7 100       18 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     6 if (scalar(keys %{$users->{whitelist}})
  6         31  
30             && (!exists($users->{whitelist}{$id})))
31             {
32 1         6 $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         10 my $channels = $self->channels;
38 5 50       11 if (keys %$channels) {
39 5   33     23 my $id = $record->{channel}{fqid} // $record->{channel}{id} // do {
      66        
40 1         5 $log->info("$name: chat id is not present");
41 1         5 return;
42             };
43 4 100       11 if (exists $channels->{blacklist}{$id}) {
44 1         9 $log->info("$name: chat '$id' is blacklisted, blocking");
45 1         5 return;
46             }
47 3 100 100     4 if (scalar(keys %{$channels->{whitelist}})
  3         19  
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         14 $log->info("$name: no reason to block, allowing");
56 2         34 return $record;
57             } ## end sub process
58              
59             42;