2021-06-19 06:23:34 +02:00
|
|
|
# File: StdinReader.pm
|
|
|
|
#
|
|
|
|
# Purpose: Reads input from STDIN.
|
|
|
|
|
2017-03-05 22:33:31 +01:00
|
|
|
# This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
# License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
|
2010-03-17 07:36:54 +01:00
|
|
|
package PBot::StdinReader;
|
2020-02-08 20:04:13 +01:00
|
|
|
use parent 'PBot::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
|
|
|
|
|
|
|
# decode STDIN input from utf8
|
|
|
|
$input = decode('UTF-8', $input);
|
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
chomp $input;
|
|
|
|
|
|
|
|
# make sure we're in the foreground first
|
|
|
|
$self->{foreground} = (tcgetpgrp($self->{tty_fd}) == getpgrp()) ? 1 : 0;
|
|
|
|
return if not $self->{foreground};
|
|
|
|
|
|
|
|
$self->{pbot}->{logger}->log("---------------------------------------------\n");
|
|
|
|
$self->{pbot}->{logger}->log("Got STDIN: $input\n");
|
|
|
|
|
|
|
|
my ($from, $text);
|
|
|
|
|
2021-06-07 04:12:14 +02:00
|
|
|
my $botnick = $self->{pbot}->{registry}->get_value('irc', 'botnick');
|
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
if ($input =~ m/^~([^ ]+)\s+(.*)/) {
|
|
|
|
$from = $1;
|
2021-06-07 04:12:14 +02:00
|
|
|
$text = "$botnick $2";
|
2020-02-15 23:38:32 +01:00
|
|
|
} else {
|
|
|
|
$from = 'stdin@pbot';
|
2021-06-07 04:12:14 +02:00
|
|
|
$text = "$botnick $input";
|
2020-02-15 23:38:32 +01:00
|
|
|
}
|
2021-06-07 04:12:14 +02:00
|
|
|
|
|
|
|
return $self->{pbot}->{interpreter}->process_line($from, $botnick, "stdin", "pbot", $text);
|
2010-03-17 07:36:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
1;
|