3
0
mirror of https://github.com/pragma-/pbot.git synced 2024-12-23 11:12:42 +01:00

Update Plang submodule and Plugin

This commit is contained in:
Pragmatic Software 2020-08-21 22:28:23 -07:00
parent e43e43458d
commit 8a1e821bf7
2 changed files with 38 additions and 24 deletions

2
Plang

@ -1 +1 @@
Subproject commit 1d68bfb4b5382cb67a8733f879aeec9d860f2e05
Subproject commit 56528a5f36e2a60d773f52e73554504cf51b70ce

View File

@ -30,6 +30,7 @@ sub initialize {
require "$path/Grammar.pm";
require "$path/Parser.pm";
require "$path/Lexer.pm";
require "$path/Types.pm";
# regset plang.debug 0-10 -- Plugin must be reloaded for this value to take effect.
my $debug = $self->{pbot}->{registry}->get_value('plang', 'debug') // 0;
@ -40,29 +41,46 @@ sub initialize {
# register some PBot-specific built-in functions
$self->{plang}->add_builtin_function('factset',
# parameters are [['param1 name', default arg], ['param2 name', default arg], ...]
[['String', 'channel', undef], ['String', 'keyword', undef], ['String', 'text', undef]],
'String', # return type
[
[['TYPE', 'String'], 'channel', undef],
[['TYPE', 'String'], 'keyword', undef],
[['TYPE', 'String'], 'text', undef]
],
['TYPE', 'String'], # return type
sub { $self->plang_builtin_factset(@_) });
$self->{plang}->add_builtin_function('factget',
[['String', 'channel', undef], ['String', 'keyword', undef], ['String', 'meta', ['STRING', 'action']]],
'String',
[
[['TYPE', 'String'], 'channel', undef],
[['TYPE', 'String'], 'keyword', undef],
[['TYPE', 'String'], 'meta', [['TYPE', 'String'], 'action']]
],
['TYPE', 'String'],
sub { $self->plang_builtin_factget(@_) });
$self->{plang}->add_builtin_function('factappend',
[['String', 'channel', undef], ['String', 'keyword', undef], ['String', 'text', undef]],
'String',
[
[['TYPE', 'String'], 'channel', undef],
[['TYPE', 'String'], 'keyword', undef],
[['TYPE', 'String'], 'text', undef]
],
['TYPE', 'String'],
sub { $self->plang_builtin_factappend(@_) });
$self->{plang}->add_builtin_function('userget',
[['String', 'name', undef]],
'Map',
[
[['TYPE', 'String'], 'name', undef]
],
['TYPELIST', [['TYPE', 'Map'], ['TYPE', 'Null']]],
sub { $self->plang_builtin_userget(@_) });
# override the built-in `print` function to send to our output buffer instead
$self->{plang}->add_builtin_function('print',
[['Any', 'expr', undef], ['String', 'end', ['STRING', "\n"]]],
'Null',
[
[['TYPE', 'Any'], 'expr', undef],
[['TYPE', 'String'], 'end', [['TYPE', 'String'], "\n"]]
],
['TYPE', 'Null'],
sub { $self->plang_builtin_print(@_) });
# register the `plang` command
@ -130,7 +148,7 @@ sub plang_builtin_print {
my ($self, $plang, $context, $name, $arguments) = @_;
my ($expr, $end) = ($plang->output_value($arguments->[0]), $arguments->[1]->[1]);
$self->{output} .= "$expr$end";
return ['NULL', undef];
return [['TYPE', 'Null'], undef];
}
# our custom PBot built-in functions for Plang
@ -144,50 +162,46 @@ sub plang_builtin_factget {
my ($self, $plang, $context, $name, $arguments) = @_;
my ($channel, $keyword, $meta) = ($arguments->[0]->[1], $arguments->[1]->[1], $arguments->[2]->[1]);
my $result = $self->{pbot}->{factoids}->get_meta($channel, $keyword, $meta);
return ['STRING', $result];
return [['TYPE', 'String'], $result];
}
sub plang_builtin_factset {
my ($self, $plang, $context, $name, $arguments) = @_;
my ($channel, $keyword, $text) = ($arguments->[0]->[1], $arguments->[1]->[1], $arguments->[2]->[1]);
return ['ERROR', "Factoid $channel.$keyword is locked. Cannot set."] if $self->is_locked($channel, $keyword);
die "Factoid $channel.$keyword is locked. Cannot set." if $self->is_locked($channel, $keyword);
$self->{pbot}->{factoids}->add_factoid('text', $channel, 'Plang', $keyword, $text);
return ['STRING', $text];
return [['TYPE', 'String'], $text];
}
sub plang_builtin_factappend {
my ($self, $plang, $context, $name, $arguments) = @_;
my ($channel, $keyword, $text) = ($arguments->[0]->[1], $arguments->[1]->[1], $arguments->[2]->[1]);
return ['ERROR', "Factoid $channel.$keyword is locked. Cannot append."] if $self->is_locked($channel, $keyword);
die "Factoid $channel.$keyword is locked. Cannot append." if $self->is_locked($channel, $keyword);
my $action = $self->{pbot}->{factoids}->get_meta($channel, $keyword, 'action');
$action = "" if not defined $action;
$action .= $text;
$self->{pbot}->{factoids}->add_factoid('text', $channel, 'Plang', $keyword, $action);
return ['STRING', $action];
return [['TYPE', 'String'], $action];
}
sub plang_builtin_userget {
my ($self, $plang, $context, $name, $arguments) = @_;
my ($username) = ($arguments->[0], $arguments->[1]);
if ($username->[0] ne 'STRING') {
$plang->error($context, "`name` argument must be a String (got " . $plang->pretty_type($username) . ")");
}
my $user = $self->{pbot}->{users}->{users}->get_data($username->[1]);
if (not defined $user) {
return ['NULL', undef];
return [['TYPE', 'Null'], undef];
}
my $hash = { %$user };
$hash->{password} = '<private>';
while (my ($key, $value) = each %$hash) {
$hash->{$key} = ['STRING', $value];
$hash->{$key} = [['TYPE', 'String'], $value];
}
return ['MAP', $hash];
return [['TYPE', 'Map'], $hash];
}
1;