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