2021-06-19 06:23:34 +02:00
|
|
|
# File: StdinReader.pm
|
|
|
|
#
|
|
|
|
# Purpose: Reads input from STDIN.
|
2021-06-25 04:28:03 +02:00
|
|
|
#
|
|
|
|
# Note: To execute a command in a channel, use the `in` command:
|
|
|
|
#
|
|
|
|
# in #foo echo hi
|
|
|
|
#
|
|
|
|
# The above will output "hi" in channel #foo.
|
2021-06-19 06:23:34 +02:00
|
|
|
|
2023-02-21 06:31:52 +01:00
|
|
|
# SPDX-FileCopyrightText: 2005-2023 Pragmatic Software <pragma78@gmail.com>
|
2021-07-11 00:00:22 +02:00
|
|
|
# SPDX-License-Identifier: MIT
|
2017-03-05 22:33:31 +01:00
|
|
|
|
2021-07-21 07:44:51 +02:00
|
|
|
package PBot::Core::StdinReader;
|
|
|
|
use parent 'PBot::Core::Class';
|
2010-03-17 07:36:54 +01:00
|
|
|
|
2021-06-19 06:23:34 +02:00
|
|
|
use PBot::Imports;
|
2019-07-11 03:40:53 +02:00
|
|
|
|
2021-06-07 04:12:14 +02:00
|
|
|
use POSIX qw(tcgetpgrp getpgrp); # to check whether process is in background or foreground
|
|
|
|
|
|
|
|
use Encode;
|
2010-03-17 07:36:54 +01:00
|
|
|
|
2014-03-14 06:51:15 +01:00
|
|
|
sub initialize {
|
2020-02-15 23:38:32 +01:00
|
|
|
my ($self, %conf) = @_;
|
|
|
|
|
2021-06-07 04:12:14 +02:00
|
|
|
# create stdin bot-admin account for bot
|
2020-02-15 23:38:32 +01:00
|
|
|
my $user = $self->{pbot}->{users}->find_user('.*', '*!stdin@pbot');
|
2021-06-07 04:12:14 +02:00
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
if (not defined $user or not $self->{pbot}->{capabilities}->userhas($user, 'botowner')) {
|
|
|
|
my $botnick = $self->{pbot}->{registry}->get_value('irc', 'botnick');
|
|
|
|
$self->{pbot}->{logger}->log("Adding stdin botowner *!stdin\@pbot...\n");
|
|
|
|
$self->{pbot}->{users}->add_user($botnick, '.*', '*!stdin@pbot', 'botowner', undef, 1);
|
|
|
|
$self->{pbot}->{users}->login($botnick, "$botnick!stdin\@pbot", undef);
|
|
|
|
$self->{pbot}->{users}->save;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (not $self->{pbot}->{registry}->get_value('general', 'daemon')) {
|
2021-06-07 04:12:14 +02:00
|
|
|
# TTY is used to check whether process is in background or foreground
|
2020-02-15 23:38:32 +01:00
|
|
|
open TTY, "</dev/tty" or die $!;
|
|
|
|
$self->{tty_fd} = fileno(TTY);
|
2021-06-07 04:12:14 +02:00
|
|
|
|
|
|
|
# add STDIN to select handler
|
2020-02-15 23:38:32 +01:00
|
|
|
$self->{pbot}->{select_handler}->add_reader(\*STDIN, sub { $self->stdin_reader(@_) });
|
|
|
|
} else {
|
|
|
|
$self->{pbot}->{logger}->log("Starting in daemon mode.\n");
|
2021-06-07 04:12:14 +02:00
|
|
|
# TODO: close STDIN, etc?
|
2020-02-15 23:38:32 +01:00
|
|
|
}
|
2014-03-14 06:51:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sub stdin_reader {
|
2020-02-15 23:38:32 +01:00
|
|
|
my ($self, $input) = @_;
|
2021-06-07 04:12:14 +02:00
|
|
|
|
2021-06-22 22:37:01 +02:00
|
|
|
# make sure we're in the foreground first
|
|
|
|
$self->{foreground} = (tcgetpgrp($self->{tty_fd}) == getpgrp()) ? 1 : 0;
|
|
|
|
return if not $self->{foreground};
|
|
|
|
|
2021-06-07 04:12:14 +02:00
|
|
|
# decode STDIN input from utf8
|
|
|
|
$input = decode('UTF-8', $input);
|
|
|
|
|
2021-06-25 04:28:03 +02:00
|
|
|
# remove newline
|
2020-02-15 23:38:32 +01:00
|
|
|
chomp $input;
|
|
|
|
|
2021-07-02 09:09:32 +02:00
|
|
|
return if not length $input;
|
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
$self->{pbot}->{logger}->log("---------------------------------------------\n");
|
|
|
|
$self->{pbot}->{logger}->log("Got STDIN: $input\n");
|
|
|
|
|
2021-06-07 04:12:14 +02:00
|
|
|
my $botnick = $self->{pbot}->{registry}->get_value('irc', 'botnick');
|
|
|
|
|
2021-06-25 04:28:03 +02:00
|
|
|
# process input as a bot command
|
2023-01-27 20:48:01 +01:00
|
|
|
return $self->{pbot}->{interpreter}->process_line('stdin@pbot', $botnick, "stdin", "pbot", $input, undef, 1);
|
2010-03-17 07:36:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
1;
|