3
0
mirror of https://github.com/pragma-/pbot.git synced 2024-12-28 21:52:35 +01:00
pbot/applets/pbot-vm/host/lib/Languages/_default.pm

466 lines
13 KiB
Perl
Raw Normal View History

#!/usr/bin/perl
2021-07-11 00:00:22 +02:00
# SPDX-FileCopyrightText: 2021 Pragmatic Software <pragma78@gmail.com>
# SPDX-License-Identifier: MIT
use 5.020;
use warnings;
use strict;
use feature qw(switch unicode_strings signatures);
no warnings qw(experimental::smartmatch experimental::signatures);
package Languages::_default;
2019-07-11 03:40:53 +02:00
use Encode;
use JSON::XS;
use Getopt::Long qw(GetOptionsFromArray :config pass_through no_ignore_case no_auto_abbrev);
use Time::HiRes qw(gettimeofday);
use FindBin qw($RealBin);
use InteractiveEdit;
use Paste;
use SplitLine;
sub new {
2022-02-07 04:32:34 +01:00
my ($class, %conf) = @_;
my $self = bless {}, $class;
%$self = %conf;
2022-02-07 04:32:34 +01:00
$self->{debug} //= 0;
$self->{arguments} //= '';
$self->{default_options} //= '';
$self->{max_history} //= 10000;
2022-02-07 04:32:34 +01:00
$self->initialize(%conf);
# remove leading and trailing whitespace
$self->{nick} =~ s/^\s+|\s+$//g;
$self->{channel} =~ s/^\s+|\s+$//g;
$self->{lang} =~ s/^\s+|\s+$//g;
2022-02-07 04:32:34 +01:00
return $self;
}
sub initialize($self, %conf) {}
sub process_interactive_edit($self) {
return interactive_edit($self);
}
sub process_standard_options($self) {
my @opt_args = split_line($self->{code}, preserve_escapes => 1, keep_spaces => 0);
my $getopt_error;
local $SIG{__WARN__} = sub {
$getopt_error = shift;
chomp $getopt_error;
};
my ($info, $arguments, $paste);
GetOptionsFromArray(\@opt_args,
'info!' => \$info,
'args|arguments=s' => \$arguments,
'paste!' => \$paste);
if ($info) {
my $cmdline = $self->{cmdline};
if (length $self->{default_options}) {
$cmdline =~ s/\$options/$self->{default_options}/;
} else {
$cmdline =~ s/\$options\s+//;
}
$cmdline =~ s/\$sourcefile/$self->{sourcefile}/g;
$cmdline =~ s/\$execfile/$self->{execfile}/g;
my $name = exists $self->{name} ? $self->{name} : $self->{lang};
print "$name cmdline: $cmdline\n";
exit;
}
if (defined $arguments) {
if (not $arguments =~ s/^"(.*)"$/$1/) {
$arguments =~ s/^'(.*)'$/$1/;
}
$self->{arguments} = $arguments;
}
if ($paste) {
$self->add_option("-paste");
}
$self->{code} = join ' ', @opt_args;
if ($self->{code} =~ s/-stdin[ =]?(.*)$//) {
$self->add_option("-stdin", $1);
}
}
sub process_custom_options {}
sub process_cmdline_options($self) {
my $code = $self->{code};
$self->{cmdline_options} = "";
while ($code =~ s/^\s*(-[^ ]+)\s*//) {
$self->{cmdline_options} .= "$1 ";
$self->add_option($1);
}
$self->{cmdline_options} =~ s/\s$//;
$self->{code} = $code;
}
2022-02-15 20:40:06 +01:00
sub add_option($self, $option, $value = '') {
$self->{options_order} //= [];
$self->{options}->{$option} = $value;
push @{$self->{options_order}}, $option;
}
sub pretty_format($self, $code) {
return $code;
}
sub preprocess_code($self, %opts) {
2022-02-07 04:32:34 +01:00
if ($self->{only_show}) {
print "$self->{code}\n";
exit;
}
2022-02-07 04:32:34 +01:00
unless($self->{got_run} and $self->{copy_code}) {
open LOG, ">> $RealBin/../log.txt";
print LOG localtime() . "\n";
print LOG "$self->{nick} $self->{channel}: [" . $self->{arguments} . "] " . $self->{cmdline_options} . "$self->{code}\n";
close LOG;
}
2022-02-07 04:32:34 +01:00
# replace \n outside of quotes with literal newline
my $new_code = "";
use constant {
NORMAL => 0,
DOUBLE_QUOTED => 1,
SINGLE_QUOTED => 2,
};
my $state = NORMAL;
my $escaped = 0;
my @chars = split //, $self->{code};
foreach my $ch (@chars) {
given ($ch) {
when ('\\') {
if ($escaped == 0) {
2022-02-07 04:32:34 +01:00
$escaped = 1;
next;
}
}
if ($state == NORMAL) {
2022-02-07 04:32:34 +01:00
when ($_ eq '"' and not $escaped) {
$state = DOUBLE_QUOTED;
}
when ($_ eq "'" and not $escaped) {
$state = SINGLE_QUOTED;
}
when ($_ eq 'n' and $escaped == 1) {
$ch = "\n";
$escaped = 0;
}
}
if ($state == DOUBLE_QUOTED) {
2022-02-07 04:32:34 +01:00
when ($_ eq '"' and not $escaped) {
$state = NORMAL;
}
}
if ($state == SINGLE_QUOTED) {
2022-02-07 04:32:34 +01:00
when ($_ eq "'" and not $escaped) {
$state = NORMAL;
}
}
}
2022-02-07 04:32:34 +01:00
$new_code .= '\\' and $escaped = 0 if $escaped;
$new_code .= $ch;
}
2022-02-07 04:32:34 +01:00
if (!$opts{omit_prelude} && exists $self->{prelude}) {
$self->{code} = "$self->{prelude}\n$self->{code}";
}
2022-02-07 04:32:34 +01:00
$self->{code} = $new_code;
}
sub execute {
2022-02-07 04:32:34 +01:00
my ($self) = @_;
my $input = $self->{'vm-input'};
my $output = $self->{'vm-output'};
2022-02-07 04:32:34 +01:00
my $date = time;
my $stdin = $self->{options}->{'-stdin'};
2019-06-13 06:35:04 +02:00
2022-02-07 04:32:34 +01:00
if (not length $stdin) {
$stdin = `fortune -u -s`;
$stdin =~ s/[\n\r\t]/ /msg;
$stdin =~ s/:/ - /g;
$stdin =~ s/\s+/ /g;
$stdin =~ s/^\s+//;
$stdin =~ s/\s+$//;
}
2019-06-13 06:35:04 +02:00
2022-02-07 04:32:34 +01:00
$stdin =~ s/(?<!\\)\\n/\n/mg;
$stdin =~ s/(?<!\\)\\r/\r/mg;
$stdin =~ s/(?<!\\)\\t/\t/mg;
$stdin =~ s/(?<!\\)\\b/\b/mg;
$stdin =~ s/(?<!\\)\\x([a-f0-9]+)/chr hex $1/igme;
$stdin =~ s/(?<!\\)\\([0-7]+)/chr oct $1/gme;
2019-06-13 06:35:04 +02:00
2022-02-07 04:32:34 +01:00
my $pretty_code = $self->pretty_format($self->{code});
2019-06-13 06:35:04 +02:00
my $cmdline = $self->{cmdline};
2022-02-07 04:32:34 +01:00
$cmdline =~ s/\$sourcefile/$self->{sourcefile}/g;
$cmdline =~ s/\$execfile/$self->{execfile}/g;
2019-06-13 06:35:04 +02:00
2022-02-07 04:32:34 +01:00
my $options = length $self->{cmdline_options} ? $self->{cmdline_options} : $self->{default_options};
if ((not exists $self->{options}->{'-paste'}) and (not defined $self->{got_run} or $self->{got_run} ne 'paste')) {
if (exists $self->{options_nopaste}) {
$options .= ' ' if length $options;
$options .= $self->{options_nopaste};
}
} else {
if (exists $self->{options_paste}) {
$options .= ' ' if length $options;
$options .= $self->{options_paste};
}
}
2022-02-07 04:32:34 +01:00
if (length $options) {
$cmdline =~ s/\$options/$options/;
} else {
$cmdline =~ s/\$options\s+//;
}
2022-02-07 04:32:34 +01:00
open LOG, ">> $RealBin/../log.txt";
print LOG "---------------------executing---------------------------------------------------\n";
print LOG localtime() . "\n";
print LOG "$cmdline\n$stdin\n$pretty_code\n";
2022-02-07 04:32:34 +01:00
my $compile_in = {
lang => $self->{lang},
sourcefile => $self->{sourcefile},
execfile => $self->{execfile},
cmdline => $cmdline,
input => $stdin,
date => $date,
arguments => $self->{arguments},
code => $pretty_code
};
2022-02-07 04:32:34 +01:00
$compile_in->{'factoid'} = $self->{'factoid'} if length $self->{'factoid'};
$compile_in->{'persist-key'} = $self->{'persist-key'} if length $self->{'persist-key'};
2022-02-07 04:32:34 +01:00
my $compile_json = encode_json($compile_in);
$compile_json .= encode('UTF-8', "\n:end:\n");
2022-02-07 04:32:34 +01:00
my $length = length $compile_json;
my $sent = 0;
my $chunk_max = 4096;
my $chunk_size = $length < $chunk_max ? $length : $chunk_max;
my $chunks_sent = 0;
2022-02-07 04:32:34 +01:00
#print LOG "Sending $length bytes [$compile_json] to vm_server\n";
2022-02-07 04:32:34 +01:00
$chunk_size -= 1; # account for newline in syswrite
2022-02-07 04:32:34 +01:00
while ($chunks_sent < $length) {
my $chunk = substr $compile_json, $chunks_sent, $chunk_size;
2022-02-07 04:32:34 +01:00
$chunks_sent += length $chunk;
my $ret = syswrite($input, $chunk);
2022-02-07 04:32:34 +01:00
if (not defined $ret) {
print STDERR "Error sending: $!\n";
2022-02-07 04:32:34 +01:00
print LOG "Error sending: $!\n";
last;
}
2022-02-07 04:32:34 +01:00
if ($ret == 0) {
print STDERR "Sent 0 bytes. Sleep 1 sec and try again\n";
2022-02-07 04:32:34 +01:00
print LOG "Sent 0 bytes. Sleep 1 sec and try again\n";
sleep 1;
next;
}
2022-02-07 04:32:34 +01:00
$sent += $ret;
}
2022-02-07 04:32:34 +01:00
close LOG;
2022-02-07 04:32:34 +01:00
my $result = "";
my $got_result = 0;
while (my $line = <$output>) {
2022-02-07 04:32:34 +01:00
utf8::decode($line);
$line =~ s/[\r\n]+$//;
last if $line =~ /^result:end$/;
if ($line =~ /^result:/) {
2022-02-07 04:32:34 +01:00
$line =~ s/^result://;
my $compile_out = decode_json($line);
$result .= "$compile_out->{result}\n";
$got_result = 1;
next;
}
if ($got_result) {
2022-02-07 04:32:34 +01:00
$result .= "$line\n";
}
}
close $input;
2022-02-07 04:32:34 +01:00
$self->{output} = $result;
2022-02-07 04:32:34 +01:00
return $result;
}
sub postprocess_output($self) {
unless($self->{got_run} and $self->{copy_code}) {
open LOG, ">> $RealBin/../log.txt";
print LOG "--------------------------post processing----------------------------------------------\n";
print LOG localtime() . "\n";
print LOG "$self->{output}\n";
close LOG;
}
# backspace
my $boutput = "";
my $active_position = 0;
$self->{output} =~ s/\n$//;
while ($self->{output} =~ /(.)/gms) {
my $c = $1;
if ($c eq "\b") {
if (--$active_position <= 0) {
$active_position = 0;
}
next;
}
substr($boutput, $active_position++, 1) = $c;
}
$self->{output} = $boutput;
my @beeps = qw/*BEEP* *BING* *DING* *DONG* *CLUNK* *BONG* *PING* *BOOP* *BLIP* *BOP* *WHIRR*/;
$self->{output} =~ s/\007/$beeps[rand @beeps]/g;
2022-02-07 04:32:34 +01:00
}
sub show_output($self) {
my $output = $self->{output};
unless ($self->{got_run} and $self->{copy_code}) {
open LOG, ">> $RealBin/../log.txt";
print LOG "------------------------show output------------------------------------------------\n";
print LOG localtime() . "\n";
print LOG "$output\n";
print LOG "========================================================================\n";
close LOG;
}
if (exists $self->{options}->{'-paste'} or (defined $self->{got_run} and $self->{got_run} eq 'paste')) {
my $cmdline = "command: $self->{cmdline}\n";
2022-02-07 04:32:34 +01:00
$cmdline =~ s/\$sourcefile/$self->{sourcefile}/g;
$cmdline =~ s/\$execfile/$self->{execfile}/g;
my $options;
if (length $self->{cmdline_options}) {
$options = $self->{cmdline_options};
2022-02-07 04:32:34 +01:00
} else {
$options = $self->{default_options};
2022-02-07 04:32:34 +01:00
}
if (exists $self->{options_paste}) {
$options .= ' ' if length $options;
$options .= $self->{options_paste};
2022-02-07 04:32:34 +01:00
}
if (length $options) {
$cmdline =~ s/\$options/$options/;
2022-02-07 04:32:34 +01:00
} else {
$cmdline =~ s/\$options\s+//;
2022-02-07 04:32:34 +01:00
}
if (length $self->{arguments}) {
$cmdline .= "arguments: $self->{arguments}\n";
}
if ($self->{options}->{'-stdin'}) {
$cmdline .= "stdin: $self->{options}->{'-stdin'}\n";
}
my $pretty_code = $self->pretty_format($self->{code});
2022-02-07 04:32:34 +01:00
my $cmdline_opening_comment = $self->{cmdline_opening_comment} // "/************* CMDLINE *************\n";
my $cmdline_closing_comment = $self->{cmdline_closing_comment} // "************** CMDLINE *************/\n";
2022-02-07 04:32:34 +01:00
my $output_opening_comment = $self->{output_opening_comment} // "/************* OUTPUT *************\n";
my $output_closing_comment = $self->{output_closing_comment} // "************** OUTPUT *************/\n";
2022-02-07 04:32:34 +01:00
$pretty_code .= "\n\n";
$pretty_code .= $cmdline_opening_comment;
$pretty_code .= "$cmdline";
$pretty_code .= $cmdline_closing_comment;
$output =~ s/\s+$//;
$pretty_code .= "\n";
$pretty_code .= $output_opening_comment;
$pretty_code .= "$output\n";
$pretty_code .= $output_closing_comment;
2022-02-07 04:32:34 +01:00
my $uri = $self->paste_0x0($pretty_code);
print "$uri\n";
exit 0;
2022-02-07 04:32:34 +01:00
}
if ($self->{channel} =~ m/^#/ and length $output > 22 and open LOG, "< $RealBin/../history/$self->{channel}-$self->{lang}.last-output") {
my $last_output;
my $time = <LOG>;
2022-02-07 04:32:34 +01:00
if (gettimeofday - $time > 60 * 4) {
close LOG;
} else {
while (my $line = <LOG>) {
$last_output .= $line;
2022-02-07 04:32:34 +01:00
}
close LOG;
2022-02-07 04:32:34 +01:00
if ((not $self->{factoid}) and defined $last_output and $last_output eq $output) {
print "Same output.\n";
2022-02-07 04:32:34 +01:00
exit 0;
}
}
}
print "$output\n";
2019-06-13 06:35:04 +02:00
open LOG, "> $RealBin/../history/$self->{channel}-$self->{lang}.last-output" or die "Couldn't open $self->{channel}-$self->{lang}.last-output: $!";
my $now = gettimeofday;
print LOG "$now\n";
print LOG "$output";
close LOG;
2019-06-13 06:35:04 +02:00
}
1;