mirror of
https://github.com/pragma-/pbot.git
synced 2024-11-26 05:49:27 +01:00
Add RunCommand plugin
This commit is contained in:
parent
79c26366f6
commit
c393976ab1
58
lib/PBot/Plugin/RunCommand.pm
Normal file
58
lib/PBot/Plugin/RunCommand.pm
Normal file
@ -0,0 +1,58 @@
|
||||
# File: RunCommand.pm
|
||||
#
|
||||
# Purpose: Runs a command, streaming each line of output in real-time.
|
||||
|
||||
# SPDX-FileCopyrightText: 2021 Pragmatic Software <pragma78@gmail.com>
|
||||
# SPDX-License-Identifier: MIT
|
||||
|
||||
package PBot::Plugin::RunCommand;
|
||||
use parent 'PBot::Plugin::Base';
|
||||
|
||||
use PBot::Imports;
|
||||
|
||||
use IPC::Run qw/start pump/;
|
||||
|
||||
sub initialize {
|
||||
my ($self, %conf) = @_;
|
||||
|
||||
$self->{pbot}->{commands}->register(sub { $self->cmd_runcmd(@_) }, "runcmd", 1);
|
||||
}
|
||||
|
||||
sub unload {
|
||||
my $self = shift;
|
||||
$self->{pbot}->{commands}->unregister("runcmd");
|
||||
}
|
||||
|
||||
sub cmd_runcmd {
|
||||
my ($self, $context) = @_;
|
||||
|
||||
my @args = $self->{pbot}->{interpreter}->split_line($context->{arguments}, strip_quotes => 1);
|
||||
|
||||
my ($in, $out, $err);
|
||||
|
||||
my $h = start \@args, \$in, \$out, \$err;
|
||||
|
||||
my $lines = 0;
|
||||
|
||||
while (pump $h) {
|
||||
if ($out =~ s/^(.*?)\n//) {
|
||||
$self->{pbot}->{conn}->privmsg($context->{from}, $1);
|
||||
$lines++;
|
||||
}
|
||||
}
|
||||
|
||||
finish $h;
|
||||
|
||||
if (length $out) {
|
||||
my @lines = split /\n/, $out;
|
||||
|
||||
foreach my $line (@lines) {
|
||||
$self->{pbot}->{conn}->privmsg($context->{from}, $line);
|
||||
$lines++;
|
||||
}
|
||||
}
|
||||
|
||||
return "No output." if not $lines;
|
||||
}
|
||||
|
||||
1;
|
Loading…
Reference in New Issue
Block a user