| 1 | package Bot::Cobalt::Plugin::PluginMgr; | ||
| 2 | $Bot::Cobalt::Plugin::PluginMgr::VERSION = '0.021003'; | ||
| 3 | ## handles and eats: !plugin | ||
| 4 | |||
| 5 | 1 | ▶ | use strictures 2; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 1 | ▶ | use v5.10; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | |||
| 8 | 1 | ▶ | use Bot::Cobalt; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | 0 | ▶ | use Bot::Cobalt::Common; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 10 | use Bot::Cobalt::Conf; | ||
| 11 | use Bot::Cobalt::Core::Loader; | ||
| 12 | |||
| 13 | use Scalar::Util qw/blessed/; | ||
| 14 | |||
| 15 | |||
| 16 | sub new { bless [], shift } | ||
| 17 | |||
| 18 | sub Cobalt_register { | ||
| 19 | my ($self, $core) = splice @_, 0, 2; | ||
| 20 | |||
| 21 | register( $self, 'SERVER', | ||
| 22 | 'public_cmd_plugin', | ||
| 23 | ); | ||
| 24 | |||
| 25 | logger->info("Registered"); | ||
| 26 | |||
| 27 | return PLUGIN_EAT_NONE | ||
| 28 | } | ||
| 29 | |||
| 30 | sub Cobalt_unregister { | ||
| 31 | my ($self, $core) = splice @_, 0, 2; | ||
| 32 | |||
| 33 | logger->info("Unregistered"); | ||
| 34 | |||
| 35 | return PLUGIN_EAT_NONE | ||
| 36 | } | ||
| 37 | |||
| 38 | sub Bot_public_cmd_plugin { | ||
| 39 | my ($self, $core) = splice @_, 0, 2; | ||
| 40 | my $msg = ${$_[0]}; | ||
| 41 | |||
| 42 | my $context = $msg->context; | ||
| 43 | my $chan = $msg->channel; | ||
| 44 | my $nick = $msg->src_nick; | ||
| 45 | |||
| 46 | my $pcfg = core()->get_plugin_cfg( $self ); | ||
| 47 | |||
| 48 | ## default to superuser-only: | ||
| 49 | my $required_lev = $pcfg->{LevelRequired} // 9999; | ||
| 50 | |||
| 51 | my $resp; | ||
| 52 | |||
| 53 | my $op = lc($msg->message_array->[0]||''); | ||
| 54 | |||
| 55 | if ( core()->auth->level($context, $nick) < $required_lev ) { | ||
| 56 | $resp = core->rpl( q{RPL_NO_ACCESS}, { nick => $nick } ); | ||
| 57 | } else { | ||
| 58 | unless ($op && grep { $_ eq $op } qw/load unload reload list/) { | ||
| 59 | |||
| 60 | broadcast( 'message', $context, $chan, | ||
| 61 | "Valid PluginMgr commands: list, load, unload, reload" | ||
| 62 | ); | ||
| 63 | |||
| 64 | return PLUGIN_EAT_ALL | ||
| 65 | } | ||
| 66 | |||
| 67 | my $method = '_cmd_plug_'.lc($op); | ||
| 68 | |||
| 69 | if ($self->can($method)) { | ||
| 70 | $resp = $self->$method($msg); | ||
| 71 | } else { | ||
| 72 | logger->error("Bug; can($method) failed in dispatcher"); | ||
| 73 | $resp = "Could not find method $method" | ||
| 74 | } | ||
| 75 | |||
| 76 | } | ||
| 77 | |||
| 78 | broadcast('message', $context, $chan, $resp) if defined $resp; | ||
| 79 | |||
| 80 | return PLUGIN_EAT_ALL | ||
| 81 | } | ||
| 82 | |||
| 83 | sub _cmd_plug_load { | ||
| 84 | my ($self, $msg) = @_; | ||
| 85 | |||
| 86 | ## !load Alias | ||
| 87 | ## !load Alias Module | ||
| 88 | |||
| 89 | my ($alias, $module) = @{ $msg->message_array }[1,2]; | ||
| 90 | |||
| 91 | return $self->_load($alias, $module) | ||
| 92 | } | ||
| 93 | |||
| 94 | sub _cmd_plug_unload { | ||
| 95 | my ($self, $msg) = @_; | ||
| 96 | |||
| 97 | ## !unload Alias | ||
| 98 | |||
| 99 | my $alias = $msg->message_array->[1]; | ||
| 100 | |||
| 101 | return $self->_unload($alias) || "Bug; no reply from _unload" | ||
| 102 | } | ||
| 103 | |||
| 104 | sub _cmd_plug_list { | ||
| 105 | my ($self, $msg) = @_; | ||
| 106 | |||
| 107 | my $pluglist = core()->plugin_list; | ||
| 108 | |||
| 109 | my @loaded = sort keys %$pluglist; | ||
| 110 | |||
| 111 | my $str = sprintf("Loaded (%d):", scalar @loaded); | ||
| 112 | while (my $plugin_alias = shift @loaded) { | ||
| 113 | $str .= ' ' . $plugin_alias; | ||
| 114 | |||
| 115 | if ($str && (length($str) > 300 || !@loaded) ) { | ||
| 116 | ## either this string has gotten long or we're done | ||
| 117 | broadcast( 'message', $msg->context, $msg->channel, $str ); | ||
| 118 | $str = ''; | ||
| 119 | } | ||
| 120 | } | ||
| 121 | } | ||
| 122 | |||
| 123 | sub _cmd_plug_reload { | ||
| 124 | my ($self, $msg) = @_; | ||
| 125 | |||
| 126 | my $alias = $msg->message_array->[1]; | ||
| 127 | |||
| 128 | my $plug_obj = core()->plugin_get($alias); | ||
| 129 | |||
| 130 | my $resp; | ||
| 131 | if (!$alias) { | ||
| 132 | |||
| 133 | broadcast( 'message', $msg->context, $msg->channel, | ||
| 134 | "Bad syntax; no plugin alias specified" | ||
| 135 | ); | ||
| 136 | |||
| 137 | return | ||
| 138 | |||
| 139 | } elsif (!$plug_obj) { | ||
| 140 | |||
| 141 | broadcast( 'message', $msg->context, $msg->channel, | ||
| 142 | core->rpl( q{RPL_PLUGIN_UNLOAD_ERR}, | ||
| 143 | plugin => $alias, | ||
| 144 | err => 'No such plugin found, is it loaded?' | ||
| 145 | ) | ||
| 146 | ); | ||
| 147 | |||
| 148 | return | ||
| 149 | |||
| 150 | } elsif (core()->State->{NonReloadable}->{$alias}) { | ||
| 151 | |||
| 152 | broadcast( 'message', $msg->context, $msg->channel, | ||
| 153 | core->rpl( q{RPL_PLUGIN_UNLOAD_ERR}, | ||
| 154 | plugin => $alias, | ||
| 155 | err => "Plugin $alias is marked as non-reloadable", | ||
| 156 | ) | ||
| 157 | ); | ||
| 158 | |||
| 159 | return | ||
| 160 | } | ||
| 161 | |||
| 162 | ## call _unload and send any response from there | ||
| 163 | my $unload_resp = $self->_unload($alias); | ||
| 164 | |||
| 165 | broadcast( 'message', $msg->context, $msg->channel, $unload_resp ); | ||
| 166 | |||
| 167 | my $pkgisa = ref $plug_obj; | ||
| 168 | |||
| 169 | return $self->_load($alias, $pkgisa); | ||
| 170 | } | ||
| 171 | |||
| 172 | sub _unload { | ||
| 173 | my ($self, $alias) = @_; | ||
| 174 | |||
| 175 | my $resp; | ||
| 176 | |||
| 177 | my $plug_obj = core()->plugin_get($alias); | ||
| 178 | my $plugisa = ref $plug_obj || return "_unload broken? no PLUGISA"; | ||
| 179 | |||
| 180 | return "Bad syntax; no plugin alias specified" | ||
| 181 | unless defined $alias; | ||
| 182 | |||
| 183 | return core->rpl( q{RPL_PLUGIN_UNLOAD_ERR}, | ||
| 184 | plugin => $alias, | ||
| 185 | err => 'No such plugin found, is it loaded?' | ||
| 186 | ) unless $plug_obj; | ||
| 187 | |||
| 188 | return core->rpl( q{RPL_PLUGIN_UNLOAD_ERR}, | ||
| 189 | plugin => $alias, | ||
| 190 | err => "Plugin $alias is marked as non-reloadable", | ||
| 191 | ) unless Bot::Cobalt::Core::Loader->is_reloadable($plug_obj); | ||
| 192 | |||
| 193 | logger->info("Attempting to unload $alias ($plugisa) per request"); | ||
| 194 | |||
| 195 | if ( core()->plugin_del($alias) ) { | ||
| 196 | delete core()->PluginObjects->{$plug_obj}; | ||
| 197 | |||
| 198 | Bot::Cobalt::Core::Loader->unload($plugisa); | ||
| 199 | |||
| 200 | ## and timers: | ||
| 201 | core()->timer_del_alias($alias); | ||
| 202 | |||
| 203 | return core->rpl( q{RPL_PLUGIN_UNLOAD}, | ||
| 204 | plugin => $alias | ||
| 205 | ) | ||
| 206 | } else { | ||
| 207 | return core->rpl( q{RPL_PLUGIN_UNLOAD_ERR}, | ||
| 208 | plugin => $alias, | ||
| 209 | err => 'Unknown core->plugin_del failure' | ||
| 210 | ) | ||
| 211 | } | ||
| 212 | |||
| 213 | return | ||
| 214 | } | ||
| 215 | |||
| 216 | sub _load { | ||
| 217 | my ($self, $alias, $module) = @_; | ||
| 218 | |||
| 219 | ## Called for !load / !reload | ||
| 220 | ## Return string for IRC | ||
| 221 | |||
| 222 | return "Bad syntax; usage: load <alias> [module]" | ||
| 223 | unless defined $alias; | ||
| 224 | |||
| 225 | return "Plugin already loaded: $alias" | ||
| 226 | if grep { $_ eq $alias } keys %{ core()->plugin_list }; | ||
| 227 | |||
| 228 | return $self->_load_module($alias, $module) | ||
| 229 | if defined $module; | ||
| 230 | |||
| 231 | my $plugin_cfg; | ||
| 232 | ## No module specified; do we know this alias? | ||
| 233 | unless ( $plugin_cfg = core()->cfg->plugins->plugin($alias) ) { | ||
| 234 | return core->rpl( q{RPL_PLUGIN_ERR}, | ||
| 235 | plugin => $alias, | ||
| 236 | err => "Plugin '$alias' not found in plugins conf", | ||
| 237 | ) | ||
| 238 | } | ||
| 239 | |||
| 240 | return $self->_load_module( | ||
| 241 | $alias, | ||
| 242 | $plugin_cfg->module | ||
| 243 | ) | ||
| 244 | } | ||
| 245 | |||
| 246 | sub _load_module { | ||
| 247 | ## _load_module( 'Auth', 'Bot::Cobalt::Plugin::Auth' ) f.ex | ||
| 248 | ## load to Core | ||
| 249 | ## returns a response string for irc | ||
| 250 | my ($self, $alias, $module) = @_; | ||
| 251 | |||
| 252 | my ($err, $obj); | ||
| 253 | try { | ||
| 254 | $obj = Bot::Cobalt::Core::Loader->load($module); | ||
| 255 | } catch { | ||
| 256 | $err = $_ | ||
| 257 | }; | ||
| 258 | |||
| 259 | if ($err) { | ||
| 260 | logger->warn("Plugin load failure; $err"); | ||
| 261 | |||
| 262 | Bot::Cobalt::Core::Loader->unload($module); | ||
| 263 | |||
| 264 | return core->rpl( q{RPL_PLUGIN_ERR}, | ||
| 265 | plugin => $alias, | ||
| 266 | err => "Module $module cannot be found/loaded: $err", | ||
| 267 | ); | ||
| 268 | } | ||
| 269 | |||
| 270 | ## store plugin objects: | ||
| 271 | core()->PluginObjects->{$obj} = $alias; | ||
| 272 | |||
| 273 | ## plugin_add returns # of plugins in pipeline on success: | ||
| 274 | if (my $loaded = core()->plugin_add( $alias, $obj ) ) { | ||
| 275 | unless ( Bot::Cobalt::Core::Loader->is_reloadable($obj) ) { | ||
| 276 | core()->State->{NonReloadable}->{$alias} = 1; | ||
| 277 | logger->debug("$alias flagged non-reloadable"); | ||
| 278 | } | ||
| 279 | |||
| 280 | my $modversion = $obj->can('VERSION') ? $obj->VERSION : 1 ; | ||
| 281 | |||
| 282 | return core->rpl( q{RPL_PLUGIN_LOAD}, | ||
| 283 | plugin => $alias, | ||
| 284 | module => $module, | ||
| 285 | version => $modversion, | ||
| 286 | ); | ||
| 287 | } else { | ||
| 288 | ## Couldn't plugin_add | ||
| 289 | logger->error("plugin_add failure for $alias"); | ||
| 290 | |||
| 291 | ## run cleanup | ||
| 292 | Bot::Cobalt::Core::Loader->unload($module); | ||
| 293 | |||
| 294 | delete core()->PluginObjects->{$obj}; | ||
| 295 | |||
| 296 | return core->rpl( q{RPL_PLUGIN_ERR}, | ||
| 297 | plugin => $alias, | ||
| 298 | err => "Unknown plugin_add failure", | ||
| 299 | ); | ||
| 300 | } | ||
| 301 | |||
| 302 | } | ||
| 303 | |||
| 304 | |||
| 305 | 1; | ||
| 306 | __END__ | ||