<commit_before># File: Plugins.pm
# Author: pragma-
#
# Purpose: Loads and manages plugins.

# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

package PBot::Plugins;
use parent 'PBot::Class';

use warnings; use strict;
use feature 'unicode_strings';

use File::Basename;

sub initialize {
  my ($self, %conf) = @_;
  $self->{plugins} = {};
  $self->{pbot}->{commands}->register(sub { $self->load_cmd(@_)   },  "plug",     1);
  $self->{pbot}->{commands}->register(sub { $self->unload_cmd(@_) },  "unplug",   1);
  $self->{pbot}->{commands}->register(sub { $self->reload_cmd(@_) },  "replug",   1);
  $self->{pbot}->{commands}->register(sub { $self->list_cmd(@_)   },  "pluglist", 0);
}

sub autoload {
  my ($self, %conf) = @_;
  return if $self->{pbot}->{registry}->get_value('plugins', 'noautoload');

  my $path = $self->{pbot}->{registry}->get_value('general', 'plugin_dir') // 'Plugins';
  my $data_dir = $self->{pbot}->{registry}->get_value('general', 'data_dir');

  $self->{pbot}->{logger}->log("Loading plugins ...\n");
  my $plugin_count = 0;

  my $fh;
  if (not open $fh, "<$data_dir/plugin_autoload") {
    $self->{pbot}->{logger}->log("warning: file $data_dir/plugin_autoload does not exist; skipping autoloading of Plugins\n");
    return;
  }

  chomp(my @plugins = <$fh>);
  close $fh;

  foreach my $plugin (sort @plugins) {
    $plugin = basename $plugin;
    $plugin =~ s/.pm$//;

    # do not load plugins that begin with a comment
    next if $plugin =~ m/^\s*#/;

    $plugin_count++ if $self->load($plugin, %conf)
  }
  $self->{pbot}->{logger}->log("$plugin_count plugin" . ($plugin_count == 1 ? '' : 's') . " loaded.\n");
}

sub load {
  my ($self, $plugin, %conf) = @_;

  $self->unload($plugin);

  return if $self->{pbot}->{registry}->get_value('plugins', 'disabled');

  my $path = $self->{pbot}->{registry}->get_value('general', 'plugin_dir') // 'Plugins';

  if (not grep { $_ eq $path } @INC) {
    unshift @INC, $path;
  }

  $self->{pbot}->{refresher}->{refresher}->refresh_module("$path/$plugin.pm");

  my $ret = eval {
    require "$path/$plugin.pm";

    if ($@) {
      chomp $@;
      $self->{pbot}->{logger}->log("Error loading $plugin: $@\n");
      return 0;
    }

    $self->{pbot}->{logger}->log("Loading $plugin\n");
    my $class = "Plugins::$plugin";
    my $mod = $class->new(pbot => $self->{pbot}, %conf);
    $self->{plugins}->{$plugin} = $mod;
    $self->{pbot}->{refresher}->{refresher}->update_cache("$path/$plugin.pm");
    return 1;<commit_msg>Plugins: minor style clean-up<commit_after># File: Plugins.pm
# Author: pragma-
#
# Purpose: Loads and manages plugins.

# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

package PBot::Plugins;
use parent 'PBot::Class';

use warnings; use strict;
use feature 'unicode_strings';

use File::Basename;

sub initialize {
  my ($self, %conf) = @_;
  $self->{plugins} = {};
  $self->{pbot}->{commands}->register(sub { $self->load_cmd(@_)   },  "plug",     1);
  $self->{pbot}->{commands}->register(sub { $self->unload_cmd(@_) },  "unplug",   1);
  $self->{pbot}->{commands}->register(sub { $self->reload_cmd(@_) },  "replug",   1);
  $self->{pbot}->{commands}->register(sub { $self->list_cmd(@_)   },  "pluglist", 0);
}

sub autoload {
  my ($self, %conf) = @_;
  return if $self->{pbot}->{registry}->get_value('plugins', 'noautoload');

  my $path = $self->{pbot}->{registry}->get_value('general', 'plugin_dir') // 'Plugins';
  my $data_dir = $self->{pbot}->{registry}->get_value('general', 'data_dir');

  $self->{pbot}->{logger}->log("Loading plugins ...\n");
  my $plugin_count = 0;

  my $fh;
  if (not open $fh, "<$data_dir/plugin_autoload") {
    $self->{pbot}->{logger}->log("warning: file $data_dir/plugin_autoload does not exist; skipping autoloading of Plugins\n");
    return;
  }

  chomp(my @plugins = <$fh>);
  close $fh;

  foreach my $plugin (sort @plugins) {
    $plugin = basename $plugin;
    $plugin =~ s/.pm$//;

    # do not load plugins that begin with a comment
    next if $plugin =~ m/^\s*#/;

    $plugin_count++ if $self->load($plugin, %conf)
  }
  $self->{pbot}->{logger}->log("$plugin_count plugin" . ($plugin_count == 1 ? '' : 's') . " loaded.\n");
}

sub load {
  my ($self, $plugin, %conf) = @_;

  $self->unload($plugin);

  return if $self->{pbot}->{registry}->get_value('plugins', 'disabled');

  my $path = $self->{pbot}->{registry}->get_value('general', 'plugin_dir') // 'Plugins';

  if (not grep { $_ eq $path } @INC) {
    unshift @INC, $path;
  }

  $self->{pbot}->{refresher}->{refresher}->refresh_module("$path/$plugin.pm");

  my $ret = eval {
    require "$path/$plugin.pm";

    if ($@) {
      chomp $@;
      $self->{pbot}->{logger}->log("Error loading $plugin: $@\n");
      return 0;
    }

    $self->{pbot}->{logger}->log("Loading $plugin\n");
    my $class = "Plugins::$plugin";
    $self->{plugins}->{$plugin} = $class->new(pbot => $self->{pbot}, %conf);
    $self->{pbot}->{refresher}->{refresher}->update_cache("$path/$plugin.pm");
    return 1;
{'fraction_non_alphanumeric': 0.22914307285768215, 'fraction_numerical': 0.004530011325028313, 'mean_word_length': 3.727029438001784, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 129, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '56958', 'n_tokens_mistral': 2075, 'n_tokens_neox': 2001, 'n_words': 427}