2020-07-09 08:21:54 +02:00
|
|
|
# File: Plang.pm
|
|
|
|
#
|
2020-09-21 08:19:47 +02:00
|
|
|
# Purpose: Scripting language for creating advanced PBot factoids
|
2020-07-09 08:21:54 +02:00
|
|
|
# and interacting with various internal PBot APIs.
|
|
|
|
|
2021-07-11 00:00:22 +02:00
|
|
|
# SPDX-FileCopyrightText: 2021 Pragmatic Software <pragma78@gmail.com>
|
|
|
|
# SPDX-License-Identifier: MIT
|
2020-07-09 08:21:54 +02:00
|
|
|
|
2021-07-14 04:45:56 +02:00
|
|
|
package PBot::Plugin::Plang;
|
|
|
|
use parent 'PBot::Plugin::Base';
|
2020-07-09 08:21:54 +02:00
|
|
|
|
2021-06-19 06:23:34 +02:00
|
|
|
use PBot::Imports;
|
2020-07-09 08:21:54 +02:00
|
|
|
|
|
|
|
sub initialize {
|
|
|
|
my ($self, %conf) = @_;
|
|
|
|
|
2022-01-18 19:59:56 +01:00
|
|
|
# load Plang module
|
2020-07-09 08:21:54 +02:00
|
|
|
my $path = $self->{pbot}->{registry}->get_value('general', 'plang_dir') // 'Plang';
|
2020-09-27 23:00:45 +02:00
|
|
|
unshift @INC, "$path/lib" if not grep { $_ eq "$path/lib" } @INC;
|
2020-07-21 04:18:05 +02:00
|
|
|
require "$path/Interpreter.pm";
|
2020-07-19 04:48:05 +02:00
|
|
|
|
2020-09-21 08:19:47 +02:00
|
|
|
# regset plang.debug <AST,VARS,FUNCS,etc>
|
|
|
|
# Plugin must be reloaded for this value to take effect.
|
|
|
|
my $debug = $self->{pbot}->{registry}->get_value('plang', 'debug') // '';
|
2020-07-12 06:15:54 +02:00
|
|
|
|
|
|
|
# create our Plang interpreter object
|
2020-07-09 08:21:54 +02:00
|
|
|
$self->{plang} = Plang::Interpreter->new(embedded => 1, debug => $debug);
|
|
|
|
|
2020-07-19 04:48:05 +02:00
|
|
|
# register some PBot-specific built-in functions
|
2020-09-21 08:19:47 +02:00
|
|
|
$self->{plang}->add_builtin_function('factset', # function name
|
|
|
|
# parameters are [[type, param1 name, default arg], [type, param2 name, default arg], ...]
|
2020-08-22 07:28:23 +02:00
|
|
|
[
|
2020-09-21 08:19:47 +02:00
|
|
|
[['TYPE', 'String'], 'channel', undef], # param 1
|
|
|
|
[['TYPE', 'String'], 'keyword', undef], # param 2
|
|
|
|
[['TYPE', 'String'], 'text', undef], # param 3
|
2020-08-22 07:28:23 +02:00
|
|
|
],
|
2020-09-21 08:19:47 +02:00
|
|
|
['TYPE', 'String'], # return type
|
|
|
|
sub { $self->plang_builtin_factset(@_) }, # builtin subref
|
|
|
|
sub { $self->plang_validate_builtin_factset(@_) } # type-checker subref
|
2020-08-25 05:46:34 +02:00
|
|
|
);
|
2020-07-13 02:36:32 +02:00
|
|
|
|
2020-08-01 03:29:50 +02:00
|
|
|
$self->{plang}->add_builtin_function('factget',
|
2020-08-22 07:28:23 +02:00
|
|
|
[
|
|
|
|
[['TYPE', 'String'], 'channel', undef],
|
|
|
|
[['TYPE', 'String'], 'keyword', undef],
|
|
|
|
[['TYPE', 'String'], 'meta', [['TYPE', 'String'], 'action']]
|
|
|
|
],
|
2020-09-30 04:22:00 +02:00
|
|
|
['TYPEUNION', [['TYPE', 'String'], ['TYPE', 'Null']]],
|
2020-08-25 05:46:34 +02:00
|
|
|
sub { $self->plang_builtin_factget(@_) },
|
|
|
|
sub { $self->plang_validate_builtin_factget(@_) },
|
|
|
|
);
|
2020-07-13 02:36:32 +02:00
|
|
|
|
2020-08-01 03:29:50 +02:00
|
|
|
$self->{plang}->add_builtin_function('factappend',
|
2020-08-22 07:28:23 +02:00
|
|
|
[
|
|
|
|
[['TYPE', 'String'], 'channel', undef],
|
|
|
|
[['TYPE', 'String'], 'keyword', undef],
|
|
|
|
[['TYPE', 'String'], 'text', undef]
|
|
|
|
],
|
|
|
|
['TYPE', 'String'],
|
2020-08-25 05:46:34 +02:00
|
|
|
sub { $self->plang_builtin_factappend(@_) },
|
|
|
|
sub { $self->plang_validate_builtin_factappend(@_) },
|
|
|
|
);
|
2020-07-23 19:51:45 +02:00
|
|
|
|
2020-08-01 03:29:50 +02:00
|
|
|
$self->{plang}->add_builtin_function('userget',
|
2020-08-22 07:28:23 +02:00
|
|
|
[
|
|
|
|
[['TYPE', 'String'], 'name', undef]
|
|
|
|
],
|
2020-09-19 22:15:03 +02:00
|
|
|
['TYPEUNION', [['TYPE', 'Map'], ['TYPE', 'Null']]],
|
2020-08-25 05:46:34 +02:00
|
|
|
sub { $self->plang_builtin_userget(@_) },
|
|
|
|
sub { $self->plang_validate_builtin_userget(@_) },
|
|
|
|
);
|
2020-07-12 06:15:54 +02:00
|
|
|
|
2020-07-19 04:48:05 +02:00
|
|
|
# override the built-in `print` function to send to our output buffer instead
|
2020-08-01 03:29:50 +02:00
|
|
|
$self->{plang}->add_builtin_function('print',
|
2020-08-22 07:28:23 +02:00
|
|
|
[
|
|
|
|
[['TYPE', 'Any'], 'expr', undef],
|
|
|
|
[['TYPE', 'String'], 'end', [['TYPE', 'String'], "\n"]]
|
|
|
|
],
|
|
|
|
['TYPE', 'Null'],
|
2020-08-25 05:46:34 +02:00
|
|
|
sub { $self->plang_builtin_print(@_) },
|
|
|
|
sub { $self->plang_validate_builtin_print(@_) },
|
|
|
|
);
|
2020-07-19 04:48:05 +02:00
|
|
|
|
2020-07-12 06:15:54 +02:00
|
|
|
# register the `plang` command
|
2021-07-31 04:01:24 +02:00
|
|
|
$self->{pbot}->{commands}->register(sub { $self->cmd_plang(@_) }, 'plang');
|
2020-07-20 22:37:41 +02:00
|
|
|
|
|
|
|
# register the `plangrepl` command (does not reset environment)
|
2021-07-31 04:01:24 +02:00
|
|
|
$self->{pbot}->{commands}->register(sub { $self->cmd_plangrepl(@_) }, 'plangrepl');
|
2020-07-09 08:21:54 +02:00
|
|
|
}
|
|
|
|
|
2020-09-21 08:19:47 +02:00
|
|
|
# runs when plugin is unloaded
|
2020-07-09 08:21:54 +02:00
|
|
|
sub unload {
|
|
|
|
my $self = shift;
|
2021-07-31 04:01:24 +02:00
|
|
|
$self->{pbot}->{commands}->unregister('plang');
|
|
|
|
$self->{pbot}->{commands}->unregister('plangrepl');
|
2022-01-18 22:42:06 +01:00
|
|
|
delete $INC{"Plang/Interpreter.pm"};
|
2020-07-09 08:21:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
sub cmd_plang {
|
|
|
|
my ($self, $context) = @_;
|
|
|
|
|
2020-07-23 21:29:24 +02:00
|
|
|
my $usage = "Usage: plang <code>; see https://github.com/pragma-/Plang and https://github.com/pragma-/pbot/blob/master/doc/Plugins/Plang.md";
|
2020-07-12 06:15:54 +02:00
|
|
|
return $usage if not length $context->{arguments};
|
|
|
|
|
2020-07-20 11:46:01 +02:00
|
|
|
$self->{output} = ""; # collect output of the embedded Plang program
|
2020-07-26 10:02:36 +02:00
|
|
|
|
|
|
|
eval {
|
|
|
|
my $result = $self->{plang}->interpret_string($context->{arguments});
|
|
|
|
|
|
|
|
# check to see if we need to append final result to output
|
|
|
|
if (defined $result->[1]) {
|
|
|
|
$self->{output} .= $self->{plang}->{interpreter}->output_value($result, literal => 1);
|
2020-07-26 01:09:20 +02:00
|
|
|
}
|
2020-07-26 10:02:36 +02:00
|
|
|
};
|
|
|
|
|
2022-01-18 19:59:56 +01:00
|
|
|
if (my $exception = $@) {
|
|
|
|
$exception = $self->{plang}->{interpreter}->output_value($exception);
|
2022-01-18 22:42:06 +01:00
|
|
|
$self->{output} .= $exception;
|
2020-07-26 01:09:20 +02:00
|
|
|
}
|
2020-07-12 06:15:54 +02:00
|
|
|
|
2020-07-13 02:36:32 +02:00
|
|
|
# return the output
|
2020-07-12 06:15:54 +02:00
|
|
|
return length $self->{output} ? $self->{output} : "No output.";
|
|
|
|
}
|
|
|
|
|
2020-07-20 22:37:41 +02:00
|
|
|
sub cmd_plangrepl {
|
|
|
|
my ($self, $context) = @_;
|
|
|
|
|
2020-07-23 21:29:24 +02:00
|
|
|
my $usage = "Usage: plangrepl <code>; see https://github.com/pragma-/Plang and https://github.com/pragma-/pbot/blob/master/doc/Plugins/Plang.md";
|
2020-07-20 22:37:41 +02:00
|
|
|
return $usage if not length $context->{arguments};
|
|
|
|
|
|
|
|
$self->{output} = ""; # collect output of the embedded Plang program
|
|
|
|
|
2020-07-26 10:02:36 +02:00
|
|
|
eval {
|
|
|
|
my $result = $self->{plang}->interpret_string($context->{arguments}, repl => 1);
|
|
|
|
|
|
|
|
# check to see if we need to append final result to output
|
|
|
|
$self->{output} .= $self->{plang}->{interpreter}->output_value($result, repl => 1) if defined $result->[1];
|
|
|
|
};
|
|
|
|
|
2022-01-18 19:59:56 +01:00
|
|
|
if (my $exception = $@) {
|
|
|
|
$exception = $self->{plang}->{interpreter}->output_value($exception);
|
|
|
|
$self->{output} .= "Run-time error: unhandled exception: $exception";
|
2020-07-26 10:02:36 +02:00
|
|
|
}
|
2020-07-20 22:37:41 +02:00
|
|
|
|
|
|
|
# return the output
|
|
|
|
return length $self->{output} ? $self->{output} : "No output.";
|
|
|
|
}
|
|
|
|
|
2020-07-19 04:48:05 +02:00
|
|
|
# overridden `print` built-in
|
2020-07-23 19:51:45 +02:00
|
|
|
sub plang_builtin_print {
|
2020-07-22 01:41:45 +02:00
|
|
|
my ($self, $plang, $context, $name, $arguments) = @_;
|
2020-07-22 01:56:38 +02:00
|
|
|
my ($expr, $end) = ($plang->output_value($arguments->[0]), $arguments->[1]->[1]);
|
|
|
|
$self->{output} .= "$expr$end";
|
2020-08-22 07:28:23 +02:00
|
|
|
return [['TYPE', 'Null'], undef];
|
2020-07-19 04:48:05 +02:00
|
|
|
}
|
|
|
|
|
2020-08-25 05:46:34 +02:00
|
|
|
sub plang_validate_builtin_print {
|
|
|
|
return [['TYPE', 'Null'], undef];
|
|
|
|
}
|
|
|
|
|
2020-07-12 06:15:54 +02:00
|
|
|
# our custom PBot built-in functions for Plang
|
|
|
|
|
2020-07-13 06:18:17 +02:00
|
|
|
sub is_locked {
|
|
|
|
my ($self, $channel, $keyword) = @_;
|
2021-07-27 06:39:44 +02:00
|
|
|
return $self->{pbot}->{factoids}->{data}->get_meta($channel, $keyword, 'locked');
|
2020-07-13 06:18:17 +02:00
|
|
|
}
|
|
|
|
|
2020-07-23 19:51:45 +02:00
|
|
|
sub plang_builtin_factget {
|
2020-07-22 01:41:45 +02:00
|
|
|
my ($self, $plang, $context, $name, $arguments) = @_;
|
2020-07-23 19:51:45 +02:00
|
|
|
my ($channel, $keyword, $meta) = ($arguments->[0]->[1], $arguments->[1]->[1], $arguments->[2]->[1]);
|
2021-07-27 06:39:44 +02:00
|
|
|
my $result = $self->{pbot}->{factoids}->{data}->get_meta($channel, $keyword, $meta);
|
2020-09-30 04:22:00 +02:00
|
|
|
if (defined $result) {
|
|
|
|
return [['TYPE', 'String'], $result];
|
|
|
|
} else {
|
|
|
|
return [['TYPE', 'Null'], undef];
|
|
|
|
}
|
2020-07-12 06:15:54 +02:00
|
|
|
}
|
|
|
|
|
2020-08-25 05:46:34 +02:00
|
|
|
sub plang_validate_builtin_factget {
|
|
|
|
return [['TYPE', 'String'], ""];
|
|
|
|
}
|
|
|
|
|
2020-07-23 19:51:45 +02:00
|
|
|
sub plang_builtin_factset {
|
2020-07-22 01:41:45 +02:00
|
|
|
my ($self, $plang, $context, $name, $arguments) = @_;
|
2020-07-23 19:51:45 +02:00
|
|
|
my ($channel, $keyword, $text) = ($arguments->[0]->[1], $arguments->[1]->[1], $arguments->[2]->[1]);
|
2020-08-22 23:49:17 +02:00
|
|
|
die "Factoid $channel.$keyword is locked. Cannot set.\n" if $self->is_locked($channel, $keyword);
|
2021-08-03 03:06:50 +02:00
|
|
|
$self->{pbot}->{factoids}->{data}->add('text', $channel, 'Plang', $keyword, $text);
|
2020-08-22 07:28:23 +02:00
|
|
|
return [['TYPE', 'String'], $text];
|
2020-07-12 06:15:54 +02:00
|
|
|
}
|
2020-07-09 08:21:54 +02:00
|
|
|
|
2020-08-25 05:46:34 +02:00
|
|
|
sub plang_validate_builtin_factset {
|
|
|
|
return [['TYPE', 'String'], ""];
|
|
|
|
}
|
|
|
|
|
2020-07-23 19:51:45 +02:00
|
|
|
sub plang_builtin_factappend {
|
2020-07-22 01:41:45 +02:00
|
|
|
my ($self, $plang, $context, $name, $arguments) = @_;
|
2020-07-23 19:51:45 +02:00
|
|
|
my ($channel, $keyword, $text) = ($arguments->[0]->[1], $arguments->[1]->[1], $arguments->[2]->[1]);
|
2020-08-22 23:49:17 +02:00
|
|
|
die "Factoid $channel.$keyword is locked. Cannot append.\n" if $self->is_locked($channel, $keyword);
|
2021-07-27 06:39:44 +02:00
|
|
|
my $action = $self->{pbot}->{factoids}->{data}->get_meta($channel, $keyword, 'action');
|
2020-07-13 06:18:17 +02:00
|
|
|
$action = "" if not defined $action;
|
|
|
|
$action .= $text;
|
2021-08-03 03:06:50 +02:00
|
|
|
$self->{pbot}->{factoids}->{data}->add('text', $channel, 'Plang', $keyword, $action);
|
2020-08-22 07:28:23 +02:00
|
|
|
return [['TYPE', 'String'], $action];
|
2020-07-09 08:21:54 +02:00
|
|
|
}
|
|
|
|
|
2020-08-25 05:46:34 +02:00
|
|
|
sub plang_validate_builtin_factappend {
|
|
|
|
return [['TYPE', 'String'], ""];
|
|
|
|
}
|
|
|
|
|
2020-07-23 19:51:45 +02:00
|
|
|
sub plang_builtin_userget {
|
|
|
|
my ($self, $plang, $context, $name, $arguments) = @_;
|
|
|
|
my ($username) = ($arguments->[0], $arguments->[1]);
|
|
|
|
|
2021-07-09 23:39:35 +02:00
|
|
|
my $user = $self->{pbot}->{users}->{storage}->get_data($username->[1]);
|
2020-07-23 19:51:45 +02:00
|
|
|
|
|
|
|
if (not defined $user) {
|
2020-08-22 07:28:23 +02:00
|
|
|
return [['TYPE', 'Null'], undef];
|
2020-07-23 19:51:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
my $hash = { %$user };
|
|
|
|
$hash->{password} = '<private>';
|
|
|
|
|
|
|
|
while (my ($key, $value) = each %$hash) {
|
2020-08-22 07:28:23 +02:00
|
|
|
$hash->{$key} = [['TYPE', 'String'], $value];
|
2020-07-23 19:51:45 +02:00
|
|
|
}
|
|
|
|
|
2020-08-22 07:28:23 +02:00
|
|
|
return [['TYPE', 'Map'], $hash];
|
2020-07-23 19:51:45 +02:00
|
|
|
}
|
|
|
|
|
2020-08-25 05:46:34 +02:00
|
|
|
sub plang_validate_builtin_userget {
|
|
|
|
return [['TYPE', 'Map'], {}];
|
|
|
|
}
|
|
|
|
|
2020-07-09 08:21:54 +02:00
|
|
|
1;
|