From 62194840b2b74a4fd62f6eff08ac7084f51445a1 Mon Sep 17 00:00:00 2001 From: Pragmatic Software Date: Wed, 8 Jul 2020 23:21:54 -0700 Subject: [PATCH] Add Plang plugin --- Plugins/Plang.pm | 66 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 Plugins/Plang.pm diff --git a/Plugins/Plang.pm b/Plugins/Plang.pm new file mode 100644 index 00000000..13ca0613 --- /dev/null +++ b/Plugins/Plang.pm @@ -0,0 +1,66 @@ +# File: Plang.pm +# Author: pragma- +# +# Purpose: Simplified scripting language for creating advanced PBot factoids +# and interacting with various internal PBot APIs. + +# 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 Plugins::Plang; +use parent 'Plugins::Plugin'; + +use warnings; use strict; +use feature 'unicode_strings'; + +use Getopt::Long qw(GetOptionsFromArray); + +sub initialize { + my ($self, %conf) = @_; + + my $path = $self->{pbot}->{registry}->get_value('general', 'plang_dir') // 'Plang'; + unshift @INC, $path if not grep { $_ eq $path } @INC; + require "$path/Interpreter.pm"; + + my $debug = $self->{pbot}->{registry}->get_value('plang', 'debug') // 0; + $self->{plang} = Plang::Interpreter->new(embedded => 1, debug => $debug); + + $self->{pbot}->{commands}->register(sub { $self->cmd_plang(@_) }, "plang", 0); +} + +sub unload { + my $self = shift; + $self->{pbot}->{commands}->unregister("plang"); +} + +sub cmd_plang { + my ($self, $context) = @_; + + my $usage = "plang ; see https://github.com/pragma-/Plang"; + + my $getopt_error; + local $SIG{__WARN__} = sub { + $getopt_error = shift; + chomp $getopt_error; + }; + + my ($show_usage); + my @opt_args = $self->{pbot}->{interpreter}->split_line($context->{arguments}, strip_quotes => 1); + + Getopt::Long::Configure("bundling"); + GetOptionsFromArray( + \@opt_args, + 'h' => \$show_usage + ); + + return $usage if $show_usage; + return "/say $getopt_error -- $usage" if defined $getopt_error; + $context->{arguments} = "@opt_args"; + + my $result = $self->{plang}->interpret_string($context->{arguments}); + return "No output." if not defined $result; + return "/say $result"; +} + +1;