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   61513 use strict;
  2         11  
  2         49  
3 2     2   8 use warnings;
  2         4  
  2         79  
4             { our $VERSION = '0.009'; }
5              
6 2     2   784 use Log::Any qw< $log >;
  2         14767  
  2         7  
7              
8 2     2   4505 use Moo;
  2         18959  
  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 7664 my ($self, $record) = @_;
17 9         31 my $name = $self->name;
18              
19 9         19 my $users = $self->users;
20 9 50       22 if (keys %$users) {
21 9   66     26 my $id = $record->{sender}{id} // do {
22 2         13 $log->info("$name: sender id is not present");
23 2         14 return;
24             };
25 7 100       15 if (exists $users->{blacklist}{$id}) {
26 1         6 $log->info("$name: sender '$id' is blacklisted, blocking");
27 1         6 return;
28             }
29 6 100 100     8 if (scalar(keys %{$users->{whitelist}})
  6         23  
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       27 if (keys %$channels) {
39 5   66     32 my $id = $record->{channel}{fqid} // $record->{channel}{id} // do {
      66        
40 1         7 $log->info("$name: chat id is not present");
41 1         7 return;
42             };
43 4 100       10 if (exists $channels->{blacklist}{$id}) {
44 1         6 $log->info("$name: chat '$id' is blacklisted, blocking");
45 1         5 return;
46             }
47 3 100 100     4 if (scalar(keys %{$channels->{whitelist}})
  3         14  
48             && (!exists($channels->{whitelist}{$id})))
49             {
50 1         7 $log->info("$name: chat '$id' not whitelisted, blocking");
51 1         5 return;
52             } ## end if (scalar(keys %{$channels...}))
53             } ## end if (keys %$channels)
54              
55 2         8 $log->info("$name: no reason to block, allowing");
56 2         10 return $record;
57             } ## end sub process
58              
59             42;