| 1 | package Bot::Cobalt::Core::ContextMeta::Auth; | ||
| 2 | $Bot::Cobalt::Core::ContextMeta::Auth::VERSION = '0.021002'; | ||
| 3 | 1 | ▶ | use strictures 2; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 1 | ▶ | use Carp; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | |||
| 6 | 1 | ▶ | use Bot::Cobalt::Common ':types'; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 7 | |||
| 8 | |||
| 9 | use Moo; | ||
| 10 | extends 'Bot::Cobalt::Core::ContextMeta'; | ||
| 11 | |||
| 12 | |||
| 13 | around add => sub { | ||
| 14 | my ($orig, $self) = splice @_, 0, 2; | ||
| 15 | ## auth->add( | ||
| 16 | ## Context => $context, | ||
| 17 | ## Username => $username, | ||
| 18 | ## Host => $host, | ||
| 19 | ## Level => $level, | ||
| 20 | ## Flags => $flags, | ||
| 21 | ## Alias => $plugin_alias | ||
| 22 | ## ) | ||
| 23 | my %args = @_; | ||
| 24 | $args{lc $_} = delete $args{$_} for keys %args; | ||
| 25 | |||
| 26 | for my $required (qw/context nickname username host level/) { | ||
| 27 | unless (defined $args{$required}) { | ||
| 28 | carp "add() missing mandatory opt $required"; | ||
| 29 | return | ||
| 30 | } | ||
| 31 | } | ||
| 32 | |||
| 33 | $args{alias} = scalar caller unless defined $args{alias}; | ||
| 34 | $args{flags} = {} unless defined $args{flags}; | ||
| 35 | |||
| 36 | my $meta = { | ||
| 37 | Alias => $args{alias}, | ||
| 38 | Username => $args{username}, | ||
| 39 | Host => $args{host}, | ||
| 40 | Level => $args{level}, | ||
| 41 | Flags => $args{flags}, | ||
| 42 | }; | ||
| 43 | |||
| 44 | $self->$orig($args{context}, $args{nickname}, $meta) | ||
| 45 | }; | ||
| 46 | |||
| 47 | sub level { | ||
| 48 | my ($self, $context, $nickname) = @_; | ||
| 49 | |||
| 50 | return 0 | ||
| 51 | unless defined $context | ||
| 52 | and defined $nickname | ||
| 53 | and exists $self->_list->{$context} | ||
| 54 | and ref $self->_list->{$context}->{$nickname}; | ||
| 55 | |||
| 56 | $self->_list->{$context}->{$nickname}->{Level} // 0 | ||
| 57 | } | ||
| 58 | |||
| 59 | sub set_flag { | ||
| 60 | my ($self, $context, $nickname, $flag) = @_; | ||
| 61 | |||
| 62 | return | ||
| 63 | unless defined $context | ||
| 64 | and defined $nickname | ||
| 65 | and $flag | ||
| 66 | and exists $self->_list->{$context} | ||
| 67 | and exists $self->_list->{$context}->{$nickname}; | ||
| 68 | |||
| 69 | $self->_list->{$context}->{$nickname}->{Flags}->{$flag} = 1 | ||
| 70 | } | ||
| 71 | |||
| 72 | sub drop_flag { | ||
| 73 | my ($self, $context, $nickname, $flag) = @_; | ||
| 74 | |||
| 75 | return | ||
| 76 | unless defined $context | ||
| 77 | and defined $nickname | ||
| 78 | and $flag | ||
| 79 | and exists $self->_list->{$context} | ||
| 80 | and exists $self->_list->{$context}->{$nickname}; | ||
| 81 | |||
| 82 | delete $self->_list->{$context}->{$nickname}->{Flags}->{$flag} | ||
| 83 | } | ||
| 84 | |||
| 85 | sub has_flag { | ||
| 86 | my ($self, $context, $nickname, $flag) = @_; | ||
| 87 | |||
| 88 | return | ||
| 89 | unless defined $context | ||
| 90 | and defined $nickname | ||
| 91 | and $flag | ||
| 92 | and exists $self->_list->{$context} | ||
| 93 | and exists $self->_list->{$context}->{$nickname}; | ||
| 94 | |||
| 95 | $self->_list->{$context}->{$nickname}->{Flags}->{$flag} | ||
| 96 | } | ||
| 97 | |||
| 98 | sub flags { | ||
| 99 | my ($self, $context, $nickname) = @_; | ||
| 100 | |||
| 101 | return +{} unless exists $self->_list->{$context} | ||
| 102 | and ref $self->_list->{$context}->{$nickname} | ||
| 103 | and ref $self->_list->{$context}->{$nickname}->{Flags} | ||
| 104 | and reftype $self->_list->{$context}->{$nickname}->{Flags} eq 'HASH'; | ||
| 105 | |||
| 106 | $self->_list->{$context}->{$nickname}->{Flags} | ||
| 107 | } | ||
| 108 | |||
| 109 | { no warnings 'once'; *user = *username } | ||
| 110 | sub username { | ||
| 111 | my ($self, $context, $nickname) = @_; | ||
| 112 | |||
| 113 | return | ||
| 114 | unless defined $context | ||
| 115 | and defined $nickname | ||
| 116 | and exists $self->_list->{$context} | ||
| 117 | and ref $self->_list->{$context}->{$nickname}; | ||
| 118 | |||
| 119 | $self->_list->{$context}->{$nickname}->{Username} | ||
| 120 | } | ||
| 121 | |||
| 122 | sub host { | ||
| 123 | my ($self, $context, $nickname) = @_; | ||
| 124 | |||
| 125 | return | ||
| 126 | unless defined $context | ||
| 127 | and defined $nickname | ||
| 128 | and exists $self->_list->{$context} | ||
| 129 | and ref $self->_list->{$context}->{$nickname}; | ||
| 130 | |||
| 131 | $self->_list->{$context}->{$nickname}->{Host} | ||
| 132 | } | ||
| 133 | |||
| 134 | sub alias { | ||
| 135 | my ($self, $context, $nickname) = @_; | ||
| 136 | |||
| 137 | return | ||
| 138 | unless defined $context | ||
| 139 | and defined $nickname | ||
| 140 | and exists $self->_list->{$context} | ||
| 141 | and ref $self->_list->{$context}->{$nickname}; | ||
| 142 | |||
| 143 | $self->_list->{$context}->{$nickname}->{Alias} | ||
| 144 | } | ||
| 145 | |||
| 146 | sub move { | ||
| 147 | my ($self, $context, $old, $new) = @_; | ||
| 148 | ## User changed nicks, f.ex | ||
| 149 | |||
| 150 | return unless exists $self->_list->{$context}->{$old}; | ||
| 151 | |||
| 152 | $self->_list->{$context}->{$new} | ||
| 153 | = delete $self->_list->{$context}->{$old} | ||
| 154 | } | ||
| 155 | |||
| 156 | |||
| 157 | 1; | ||
| 158 | __END__ | ||