« blib/lib/Bot/Cobalt/Plugin/Games/Dice.pm
↑ Summary
blib/lib/Bot/Cobalt/Plugin/RDB/AsyncSearch.pm »
| 1 | package Bot::Cobalt::IRC::Role::AdminCmds; | ||
| 2 | $Bot::Cobalt::IRC::Role::AdminCmds::VERSION = '0.021002'; | ||
| 3 | use strictures 2; | ||
| 4 | use Scalar::Util 'reftype'; | ||
| 5 | |||
| 6 | use Bot::Cobalt; | ||
| 7 | use Bot::Cobalt::Common; | ||
| 8 | |||
| 9 | use Try::Tiny; | ||
| 10 | |||
| 11 | use Moo::Role; | ||
| 12 | |||
| 13 | |||
| 14 | sub Bot_public_cmd_server { | ||
| 15 | my ($self, $core) = splice @_, 0, 2; | ||
| 16 | my $msg = ${ $_[0] }; | ||
| 17 | my $context = $msg->context; | ||
| 18 | my $src_nick = $msg->src_nick; | ||
| 19 | |||
| 20 | return PLUGIN_EAT_ALL unless | ||
| 21 | $core->auth->has_flag($context, $src_nick, 'SUPERUSER'); | ||
| 22 | |||
| 23 | my $cmd = lc($msg->message_array->[0] || 'list'); | ||
| 24 | my $meth = '_cmd_'.$cmd; | ||
| 25 | unless ( $self->can($meth) ) { | ||
| 26 | broadcast message => $msg->context, $msg->channel, | ||
| 27 | "Unknown command; try one of: list, current, connect, disconnect"; | ||
| 28 | return PLUGIN_EAT_ALL | ||
| 29 | } | ||
| 30 | |||
| 31 | logger->debug("Dispatching $cmd for $src_nick"); | ||
| 32 | $self->$meth($msg) | ||
| 33 | } | ||
| 34 | |||
| 35 | sub _cmd_list { | ||
| 36 | my ($self, $msg) = @_; | ||
| 37 | my @contexts = keys %{ core->Servers }; | ||
| 38 | my $pcfg = plugin_cfg($self); | ||
| 39 | ## FIXME look for contexts that are conf'd but not active | ||
| 40 | |||
| 41 | broadcast message => $msg->context, $msg->channel, | ||
| 42 | "Active contexts: " . join ' ', @contexts ; | ||
| 43 | |||
| 44 | PLUGIN_EAT_ALL | ||
| 45 | } | ||
| 46 | |||
| 47 | sub _cmd_current { | ||
| 48 | my ($self, $msg) = @_; | ||
| 49 | |||
| 50 | broadcast message => $msg->context, $msg->channel, | ||
| 51 | "Currently on context ".$msg->context; | ||
| 52 | |||
| 53 | PLUGIN_EAT_ALL | ||
| 54 | } | ||
| 55 | |||
| 56 | { no warnings 'once'; *_cmd_reconnect = *_cmd_connect; } | ||
| 57 | sub _cmd_connect { | ||
| 58 | my ($self, $msg) = @_; | ||
| 59 | |||
| 60 | my $pcfg = plugin_cfg($self); | ||
| 61 | |||
| 62 | unless (ref $pcfg && reftype $pcfg eq 'HASH' && keys %$pcfg) { | ||
| 63 | broadcast message => $msg->context, $msg->channel, | ||
| 64 | "Could not locate any network configuration."; | ||
| 65 | logger->error("_cmd_connect could not find an IRC network cfg"); | ||
| 66 | return PLUGIN_EAT_ALL | ||
| 67 | } | ||
| 68 | |||
| 69 | my $target_ctxt = $msg->message_array->[1]; | ||
| 70 | unless (defined $target_ctxt) { | ||
| 71 | broadcast message => $msg->context, $msg->channel, | ||
| 72 | "No context specified."; | ||
| 73 | return PLUGIN_EAT_ALL | ||
| 74 | } | ||
| 75 | |||
| 76 | unless ($pcfg->{Networks}->{$target_ctxt}) { | ||
| 77 | broadcast message => $msg->context, $msg->channel, | ||
| 78 | "Could not locate configuration for context $target_ctxt"; | ||
| 79 | return PLUGIN_EAT_ALL | ||
| 80 | } | ||
| 81 | |||
| 82 | ## Do we already have this context? | ||
| 83 | if (my $ctxt_obj = irc_context($target_ctxt) ) { | ||
| 84 | if ($ctxt_obj->connected) { | ||
| 85 | broadcast message => $msg->context, $msg->channel, | ||
| 86 | "Attempting reconnect for context $target_ctxt"; | ||
| 87 | } | ||
| 88 | logger->info("Attempting reconnect for context $target_ctxt"); | ||
| 89 | broadcast ircplug_disconnect => $target_ctxt; | ||
| 90 | broadcast ircplug_connect => $target_ctxt; | ||
| 91 | broadcast ircplug_timer_serv_retry => | ||
| 92 | +{ context => $target_ctxt, delay => 300 } ; | ||
| 93 | return PLUGIN_EAT_ALL | ||
| 94 | } | ||
| 95 | |||
| 96 | broadcast message => $msg->context, $msg->channel, | ||
| 97 | "Issuing connect for context $target_ctxt"; | ||
| 98 | |||
| 99 | my $src_nick = $msg->src_nick; | ||
| 100 | my $auth_usr = core->auth->username($msg->context, $src_nick); | ||
| 101 | |||
| 102 | logger->info( | ||
| 103 | "Issuing connect for context $target_ctxt", | ||
| 104 | "(Issued by $src_nick [$auth_usr])" | ||
| 105 | ); | ||
| 106 | |||
| 107 | broadcast ircplug_connect => $target_ctxt; | ||
| 108 | |||
| 109 | return PLUGIN_EAT_ALL | ||
| 110 | } | ||
| 111 | |||
| 112 | sub _cmd_disconnect { | ||
| 113 | my ($self, $msg) = @_; | ||
| 114 | my $target_ctxt = $msg->message_array->[1]; | ||
| 115 | |||
| 116 | unless (defined $target_ctxt) { | ||
| 117 | broadcast message => $msg->context, $msg->channel, | ||
| 118 | "No context specified."; | ||
| 119 | return PLUGIN_EAT_ALL | ||
| 120 | } | ||
| 121 | |||
| 122 | my $ctxt_obj; | ||
| 123 | unless ($ctxt_obj = irc_context($target_ctxt) ) { | ||
| 124 | broadcast message => $msg->context, $msg->channel, | ||
| 125 | "Could not find context object for $target_ctxt"; | ||
| 126 | return PLUGIN_EAT_ALL | ||
| 127 | } | ||
| 128 | |||
| 129 | unless (keys %{ core->Servers } > 1) { | ||
| 130 | broadcast message => $msg->context, $msg->channel, | ||
| 131 | "Refusing disconnect; have no other active contexts."; | ||
| 132 | return PLUGIN_EAT_ALL | ||
| 133 | } | ||
| 134 | |||
| 135 | broadcast message => $msg->context, $msg->channel, | ||
| 136 | "Attempting to disconnect from $target_ctxt"; | ||
| 137 | |||
| 138 | my $src_nick = $msg->src_nick; | ||
| 139 | my $auth_usr = core->auth->username($msg->context, $src_nick); | ||
| 140 | |||
| 141 | logger->info( | ||
| 142 | "Issuing disconnect for context $target_ctxt", | ||
| 143 | "(Issued by $src_nick [$auth_usr])" | ||
| 144 | ); | ||
| 145 | |||
| 146 | broadcast ircplug_disconnect => $target_ctxt; | ||
| 147 | |||
| 148 | return PLUGIN_EAT_ALL | ||
| 149 | } | ||
| 150 | |||
| 151 | |||
| 152 | sub Bot_ircplug_timer_serv_retry { | ||
| 153 | my ($self, $core) = splice @_, 0, 2; | ||
| 154 | my $hints = ${ $_[0] }; | ||
| 155 | |||
| 156 | my $context = $hints->{context}; | ||
| 157 | my $delay = $hints->{delay} || 300; | ||
| 158 | |||
| 159 | logger->debug("ircplug_timer_serv_retry called for $context"); | ||
| 160 | my $ctxt_obj; | ||
| 161 | unless ($ctxt_obj = irc_context($context) && $ctxt_obj->connected) { | ||
| 162 | logger->info("Attempting reconnect to $context . . ."); | ||
| 163 | broadcast ircplug_connect => $context; | ||
| 164 | core->timer_set( $delay, | ||
| 165 | +{ | ||
| 166 | Event => 'ircplug_timer_serv_retry', | ||
| 167 | Args => [ +{ context => $context, delay => $delay } ], | ||
| 168 | }, | ||
| 169 | ); | ||
| 170 | } | ||
| 171 | |||
| 172 | return PLUGIN_EAT_ALL | ||
| 173 | } | ||
| 174 | |||
| 175 | 1; | ||
| 176 | __END__ |