Created
July 17, 2024 10:54
-
-
Save Ghodawalaaman/9ec311a52ef195f06ab6ee2562438bcb to your computer and use it in GitHub Desktop.
pwsh bot in perl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use strict; | |
use warnings; | |
use POE qw(Component::IRC); | |
my $nickname = 'powershell_bot' . $$; | |
my $ircname = 'powershell_bot'; | |
my $server = 'irc.libera.chat'; | |
my @channels = ('#bash', '#bottest', '#bsah'); | |
# We create a new PoCo-IRC object | |
my $irc = POE::Component::IRC->spawn( | |
nick => $nickname, | |
ircname => $ircname, | |
server => $server, | |
) or die "Oh noooo! $!"; | |
POE::Session->create( | |
package_states => [ | |
main => [ qw(_start irc_001 irc_public) ], | |
], | |
heap => { irc => $irc }, | |
); | |
$poe_kernel->run(); | |
sub _start { | |
my $heap = $_[HEAP]; | |
# retrieve our component's object from the heap where we stashed it | |
my $irc = $heap->{irc}; | |
$irc->yield( register => 'all' ); | |
$irc->yield( connect => { } ); | |
return; | |
} | |
sub irc_001 { | |
my $sender = $_[SENDER]; | |
# Since this is an irc_* event, we can get the component's object by | |
# accessing the heap of the sender. Then we register and connect to the | |
# specified server. | |
my $irc = $sender->get_heap(); | |
print "Connected to ", $irc->server_name(), "\n"; | |
# we join our channels | |
$irc->yield( join => $_ ) for @channels; | |
return; | |
} | |
sub irc_public { | |
my ($sender, $who, $where, $what) = @_[SENDER, ARG0 .. ARG2]; | |
my $nick = ( split /!/, $who )[0]; | |
my $channel = $where->[0]; | |
if ( $what =~ /^#pwsh (.*)/ ) { | |
system 'sh', '-c', 'timeout 6s docker run --net=none --rm mcr.microsoft.com/powershell pwsh -c "$1" 1>/tmp/output.txt 2>/tmp/err.txt', 'sh', $1; | |
`sed -i 's/\x1b\[[0-9;]*m//g' /tmp/output.txt`; | |
`sed -i 's/\x1b\[[0-9;]*m//g' /tmp/err.txt`; | |
my $output_url = `curl -F'file=@/tmp/output.txt' https://0x0.st`; | |
my $err_url = `curl -F'file=@/tmp/err.txt' https://0x0.st`; | |
$irc->yield( privmsg => $channel => "$nick: output: $output_url" ); | |
$irc->yield( privmsg => $channel => "$nick: err: $err_url" ); | |
} | |
return; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment