| 1 | package Bot::Cobalt::Plugin::Rehash; | ||
| 2 | $Bot::Cobalt::Plugin::Rehash::VERSION = '0.021003'; | ||
| 3 | 1 | ▶ | use strictures 2; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 1 | ▶ | use v5.10; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | |||
| 6 | 1 | ▶ | use Bot::Cobalt; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 0 | ▶ | use Bot::Cobalt::Common; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 8 | use Bot::Cobalt::Conf; | ||
| 9 | use Bot::Cobalt::Lang; | ||
| 10 | |||
| 11 | use File::Spec; | ||
| 12 | |||
| 13 | use strictures 2; | ||
| 14 | |||
| 15 | sub new { bless [], shift } | ||
| 16 | |||
| 17 | sub Cobalt_register { | ||
| 18 | my ($self, $core) = splice @_, 0, 2; | ||
| 19 | |||
| 20 | register( $self, 'SERVER', | ||
| 21 | 'rehash', 'public_cmd_rehash' | ||
| 22 | ); | ||
| 23 | |||
| 24 | logger->info("Registered, commands: !rehash"); | ||
| 25 | |||
| 26 | PLUGIN_EAT_NONE | ||
| 27 | } | ||
| 28 | |||
| 29 | sub Cobalt_unregister { | ||
| 30 | my ($self, $core) = splice @_, 0, 2; | ||
| 31 | logger->info("Unregistered"); | ||
| 32 | PLUGIN_EAT_NONE | ||
| 33 | } | ||
| 34 | |||
| 35 | sub Bot_rehash { | ||
| 36 | my ($self, $core) = splice @_, 0, 2; | ||
| 37 | |||
| 38 | $self->_rehash_core_cf; | ||
| 39 | $self->_rehash_channels_cf; | ||
| 40 | $self->_rehash_plugins_cf; | ||
| 41 | |||
| 42 | PLUGIN_EAT_NONE | ||
| 43 | } | ||
| 44 | |||
| 45 | sub Bot_public_cmd_rehash { | ||
| 46 | my ($self, $core) = splice @_, 0, 2; | ||
| 47 | my $msg = ${$_[0]}; | ||
| 48 | |||
| 49 | my $context = $msg->context; | ||
| 50 | my $channel = $msg->channel; | ||
| 51 | my $nick = $msg->src_nick; | ||
| 52 | |||
| 53 | my $auth_lev = core->auth->level($context, $nick); | ||
| 54 | my $auth_usr = core->auth->username($context, $nick); | ||
| 55 | |||
| 56 | my $pcfg = plugin_cfg($self) || {}; | ||
| 57 | my $required_lev = $pcfg->{LevelRequired} // 9999; | ||
| 58 | |||
| 59 | unless ($auth_lev >= $required_lev) { | ||
| 60 | my $resp = core->rpl( q{RPL_NO_ACCESS}, | ||
| 61 | nick => $nick | ||
| 62 | ); | ||
| 63 | |||
| 64 | broadcast 'message', $context, $channel, $resp; | ||
| 65 | |||
| 66 | return PLUGIN_EAT_ALL | ||
| 67 | } | ||
| 68 | |||
| 69 | my $type = lc($msg->message_array->[0] || 'all'); | ||
| 70 | |||
| 71 | my $meth = '_cmd_'.$type; | ||
| 72 | |||
| 73 | logger->debug("dispatching $meth for $nick ($auth_usr)"); | ||
| 74 | |||
| 75 | my $resp; | ||
| 76 | if ( $self->can($meth) ) { | ||
| 77 | ## Handlers return a response or die() : | ||
| 78 | $resp = try { | ||
| 79 | $self->$meth($msg) | ||
| 80 | } catch { | ||
| 81 | my $error = $_; | ||
| 82 | logger->error("Rehash ($type) failure; $error"); | ||
| 83 | "Failed rehash: $error" | ||
| 84 | }; | ||
| 85 | } else { | ||
| 86 | $resp = "Unknown config group, try: core, plugins, langset, channels" | ||
| 87 | } | ||
| 88 | |||
| 89 | broadcast 'message', $context, $channel, $resp; | ||
| 90 | |||
| 91 | PLUGIN_EAT_ALL | ||
| 92 | } | ||
| 93 | |||
| 94 | ## Command handlers: | ||
| 95 | sub _cmd_all { | ||
| 96 | my ($self, $msg) = @_; | ||
| 97 | |||
| 98 | $self->_rehash_core_cf; | ||
| 99 | $self->_rehash_channels_cf; | ||
| 100 | $self->_rehash_plugins_cf; | ||
| 101 | |||
| 102 | "Rehashed loaded configuration objects." | ||
| 103 | } | ||
| 104 | |||
| 105 | sub _cmd_channels { | ||
| 106 | my ($self, $msg) = @_; | ||
| 107 | |||
| 108 | $self->_rehash_channels_cf; | ||
| 109 | |||
| 110 | "Rehashed current channels configuration." | ||
| 111 | } | ||
| 112 | |||
| 113 | sub _cmd_core { | ||
| 114 | my ($self, $msg) = @_; | ||
| 115 | |||
| 116 | $self->_rehash_core_cf; | ||
| 117 | |||
| 118 | "Rehashed core configuration." | ||
| 119 | } | ||
| 120 | |||
| 121 | sub _cmd_plugins { | ||
| 122 | my ($self, $msg) = @_; | ||
| 123 | |||
| 124 | $self->_rehash_plugins_cf; | ||
| 125 | |||
| 126 | "Rehashed plugins configuration." | ||
| 127 | } | ||
| 128 | |||
| 129 | sub _cmd_langset { | ||
| 130 | my ($self, $msg) = @_; | ||
| 131 | |||
| 132 | my $lang = $msg->message_array->[1]; | ||
| 133 | |||
| 134 | $self->_rehash_langset($lang); | ||
| 135 | |||
| 136 | "Rehashed loaded language set" | ||
| 137 | } | ||
| 138 | |||
| 139 | |||
| 140 | ## Actual configuration reloaders: | ||
| 141 | sub _rehash_plugins_cf { | ||
| 142 | my ($self) = @_; | ||
| 143 | |||
| 144 | require Bot::Cobalt::Conf::File::Plugins; | ||
| 145 | |||
| 146 | my $new_cfg_obj = Bot::Cobalt::Conf::File::Plugins->new( | ||
| 147 | etcdir => core()->etc, | ||
| 148 | cfg_path => core()->cfg->plugins->cfg_path, | ||
| 149 | ); | ||
| 150 | |||
| 151 | core()->cfg->set_plugins( $new_cfg_obj ); | ||
| 152 | |||
| 153 | logger->info("Reloaded plugins.conf"); | ||
| 154 | |||
| 155 | broadcast 'rehashed', 'plugins'; | ||
| 156 | } | ||
| 157 | |||
| 158 | sub _rehash_channels_cf { | ||
| 159 | my ($self) = @_; | ||
| 160 | |||
| 161 | require Bot::Cobalt::Conf::File::Channels; | ||
| 162 | |||
| 163 | my $new_cfg_obj = Bot::Cobalt::Conf::File::Channels->new( | ||
| 164 | cfg_path => core()->cfg->channels->cfg_path, | ||
| 165 | ); | ||
| 166 | |||
| 167 | core()->cfg->set_channels( $new_cfg_obj ); | ||
| 168 | |||
| 169 | logger->info("Reloaded channels config."); | ||
| 170 | |||
| 171 | broadcast 'rehashed', 'channels'; | ||
| 172 | } | ||
| 173 | |||
| 174 | sub _rehash_core_cf { | ||
| 175 | my ($self) = @_; | ||
| 176 | |||
| 177 | require Bot::Cobalt::Conf::File::Core; | ||
| 178 | |||
| 179 | my $new_cfg_obj = Bot::Cobalt::Conf::File::Core->new( | ||
| 180 | cfg_path => core()->cfg->core->cfg_path, | ||
| 181 | ); | ||
| 182 | |||
| 183 | core()->cfg->set_core( $new_cfg_obj ); | ||
| 184 | |||
| 185 | logger->info("Reloaded core config."); | ||
| 186 | |||
| 187 | ## Bot_rehash ($type) : | ||
| 188 | broadcast 'rehashed', 'core'; | ||
| 189 | } | ||
| 190 | |||
| 191 | sub _rehash_langset { | ||
| 192 | my ($self, $langset) = @_; | ||
| 193 | |||
| 194 | ## FIXME document that you should rehash core then rehash langset | ||
| 195 | ## for updated Language: directives | ||
| 196 | |||
| 197 | $langset ||= core()->cfg->core->language; | ||
| 198 | |||
| 199 | my $lang_dir = File::Spec->catdir( core()->etc, 'langs' ); | ||
| 200 | |||
| 201 | my $lang_obj = Bot::Cobalt::Lang->new( | ||
| 202 | use_core => 1, | ||
| 203 | |||
| 204 | lang_dir => $lang_dir, | ||
| 205 | lang => $langset, | ||
| 206 | ); | ||
| 207 | |||
| 208 | die "Language set $langset has no RPLs" | ||
| 209 | unless scalar keys %{ $lang_obj->rpls } ; | ||
| 210 | |||
| 211 | core()->set_langset( $lang_obj ); | ||
| 212 | core()->set_lang( $lang_obj->rpls ); | ||
| 213 | |||
| 214 | logger->info("Reloaded core langset ($langset)"); | ||
| 215 | |||
| 216 | broadcast 'rehashed', 'langset'; | ||
| 217 | } | ||
| 218 | |||
| 219 | |||
| 220 | 1; | ||
| 221 | __END__ | ||