| 1 | package Bot::Cobalt::Core::Role::Timers; | ||
| 2 | $Bot::Cobalt::Core::Role::Timers::VERSION = '0.021002'; | ||
| 3 | use 5.10.1; | ||
| 4 | use strictures 2; | ||
| 5 | |||
| 6 | use Bot::Cobalt::Timer; | ||
| 7 | |||
| 8 | use Scalar::Util qw/blessed/; | ||
| 9 | |||
| 10 | use List::Objects::WithUtils; | ||
| 11 | |||
| 12 | use Moo::Role; | ||
| 13 | |||
| 14 | requires qw/ | ||
| 15 | log | ||
| 16 | debug | ||
| 17 | send_event | ||
| 18 | /; | ||
| 19 | |||
| 20 | |||
| 21 | ## FIXME tests are nonexistant | ||
| 22 | |||
| 23 | has TimerPool => ( | ||
| 24 | is => 'rw', | ||
| 25 | builder => sub { hash }, | ||
| 26 | ); | ||
| 27 | |||
| 28 | |||
| 29 | sub timer_gen_unique_id { | ||
| 30 | my ($self) = @_; | ||
| 31 | my @p = ( 'a'..'f', 1..9 ); | ||
| 32 | my $id = join '', map { $p[rand@p] } 1 .. 4; | ||
| 33 | $id .= $p[rand@p] while $self->TimerPool->exists($id); | ||
| 34 | $id | ||
| 35 | } | ||
| 36 | |||
| 37 | sub timer_set { | ||
| 38 | my ($self, $item) = @_; | ||
| 39 | |||
| 40 | my ($caller_pkg, $caller_line) = (caller)[0,2]; | ||
| 41 | my $d_line = "$caller_pkg line $caller_line"; | ||
| 42 | |||
| 43 | my $timer = $item; | ||
| 44 | unless (blessed $item && $item->isa('Bot::Cobalt::Timer') ) { | ||
| 45 | ## hashref-style (we hope) | ||
| 46 | $timer = $self->timer_set_hashref( $item, @_[2 .. $#_] ); | ||
| 47 | } | ||
| 48 | |||
| 49 | unless ($timer->has_id) { | ||
| 50 | $timer->id( $self->timer_gen_unique_id ); | ||
| 51 | } | ||
| 52 | |||
| 53 | ## Add to our TimerPool. | ||
| 54 | $self->TimerPool->set($timer->id => $timer); | ||
| 55 | $self->send_event( new_timer => $timer->id ); | ||
| 56 | |||
| 57 | $self->log->debug( | ||
| 58 | "timer_set; ".join ' ', $timer->id, $timer->delay, $timer->event | ||
| 59 | ) if $self->debug > 1; | ||
| 60 | |||
| 61 | $timer->id | ||
| 62 | } | ||
| 63 | |||
| 64 | sub timer_set_hashref { | ||
| 65 | my ($self, $delay, $ev, $id) = @_; | ||
| 66 | |||
| 67 | my ($caller_pkg, $caller_line) = (caller)[0,2]; | ||
| 68 | my $d_line = "$caller_pkg line $caller_line"; | ||
| 69 | |||
| 70 | unless (ref $ev eq 'HASH') { | ||
| 71 | if (ref $ev) { | ||
| 72 | $self->log->warn( | ||
| 73 | "timer_set_hashref expected HASH but got $ev; $d_line" | ||
| 74 | ); | ||
| 75 | return | ||
| 76 | } else { | ||
| 77 | ## Assume we were passed a simple string. | ||
| 78 | $ev = { Event => $ev }; | ||
| 79 | } | ||
| 80 | } | ||
| 81 | |||
| 82 | my $timer = Bot::Cobalt::Timer->new; | ||
| 83 | |||
| 84 | $timer->id($id) if $id; | ||
| 85 | |||
| 86 | ## Try to guess type, or default to 'event' | ||
| 87 | my $type = $ev->{Type}; | ||
| 88 | unless ($type) { | ||
| 89 | if (defined $ev->{Text} && defined $ev->{Context}) { | ||
| 90 | $type = 'msg' | ||
| 91 | } else { | ||
| 92 | $type = 'event' | ||
| 93 | } | ||
| 94 | } | ||
| 95 | |||
| 96 | my($event_name, @event_args); | ||
| 97 | |||
| 98 | TYPE: { | ||
| 99 | if ($type eq "event") { | ||
| 100 | unless (defined $ev->{Event}) { | ||
| 101 | $self->log->warn( | ||
| 102 | "timer_set_hashref no Event specified; $d_line" | ||
| 103 | ); | ||
| 104 | return | ||
| 105 | } | ||
| 106 | |||
| 107 | $timer->event( $ev->{Event} ); | ||
| 108 | $timer->args( $ev->{Args}//[] ); | ||
| 109 | |||
| 110 | last TYPE | ||
| 111 | } | ||
| 112 | |||
| 113 | if (grep { $type eq $_ } qw/msg message privmsg action/) { | ||
| 114 | unless (defined $ev->{Text} && defined $ev->{Target}) { | ||
| 115 | $self->log->warn( | ||
| 116 | "timer_set_hashref; $type needs Text and Target; $d_line" | ||
| 117 | ); | ||
| 118 | |||
| 119 | return | ||
| 120 | } | ||
| 121 | |||
| 122 | $timer->context( $ev->{Context} ) if defined $ev->{Context}; | ||
| 123 | $timer->target( $ev->{Target} ); | ||
| 124 | $timer->text( $ev->{Text} ); | ||
| 125 | $timer->type( $type ); | ||
| 126 | |||
| 127 | last TYPE | ||
| 128 | } | ||
| 129 | |||
| 130 | $self->log->error("Unknown type $type passed to timer_set_hashref"); | ||
| 131 | return | ||
| 132 | } | ||
| 133 | |||
| 134 | ## Tag w/ __PACKAGE__ if no alias is specified | ||
| 135 | $timer->alias( $ev->{Alias} // scalar caller ); | ||
| 136 | |||
| 137 | ## Start ticking. | ||
| 138 | $timer->delay( $delay ); | ||
| 139 | |||
| 140 | $timer | ||
| 141 | } | ||
| 142 | |||
| 143 | sub timer_get { | ||
| 144 | my ($self, $id) = @_; | ||
| 145 | return unless $id and $self->TimerPool->exists($id); | ||
| 146 | |||
| 147 | $self->log->debug("timer retrieved; $id") | ||
| 148 | if $self->debug > 1; | ||
| 149 | |||
| 150 | $self->TimerPool->get($id) | ||
| 151 | } | ||
| 152 | |||
| 153 | sub timer_get_alias { | ||
| 154 | ## get all timerIDs for this alias | ||
| 155 | my ($self, $alias) = @_; | ||
| 156 | return unless $alias; | ||
| 157 | |||
| 158 | my @timers; | ||
| 159 | $self->TimerPool->kv->visit(sub { | ||
| 160 | my ($id, $entry) = @$_; | ||
| 161 | push @timers, $id if $entry->alias eq $alias | ||
| 162 | }); | ||
| 163 | |||
| 164 | wantarray ? @timers : \@timers | ||
| 165 | } | ||
| 166 | |||
| 167 | sub timer_del { | ||
| 168 | ## delete a timer by its ID | ||
| 169 | ## doesn't care if the timerID actually exists or not. | ||
| 170 | my ($self, $id) = @_; | ||
| 171 | return unless $id; | ||
| 172 | |||
| 173 | $self->log->debug("timer del; $id") | ||
| 174 | if $self->debug > 1; | ||
| 175 | |||
| 176 | return unless $self->TimerPool->exists($id); | ||
| 177 | |||
| 178 | my $deleted = $self->TimerPool->delete($id); | ||
| 179 | $self->send_event( 'deleted_timer', $id, $deleted->all ); | ||
| 180 | |||
| 181 | $deleted->all | ||
| 182 | } | ||
| 183 | |||
| 184 | sub timer_del_alias { | ||
| 185 | my ($self, $alias) = @_; | ||
| 186 | return unless $alias; | ||
| 187 | |||
| 188 | my @deleted; | ||
| 189 | $self->TimerPool->kv->visit(sub { | ||
| 190 | my ($id, $entry) = @$_; | ||
| 191 | if ($entry->alias eq $alias) { | ||
| 192 | push @deleted, $id; | ||
| 193 | my $removed = $self->TimerPool->delete($id); | ||
| 194 | $self->send_event( deleted_timer => $id, $removed ); | ||
| 195 | } | ||
| 196 | }); | ||
| 197 | |||
| 198 | wantarray ? @deleted : scalar @deleted | ||
| 199 | } | ||
| 200 | |||
| 201 | |||
| 202 | 1; | ||
| 203 | __END__ |