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;
|
|
|
|
|
|
|
|
use warnings;
|
|
|
|
use strict;
|
|
|
|
|
2019-07-11 03:40:53 +02:00
|
|
|
use feature 'unicode_strings';
|
|
|
|
|
2010-03-17 07:36:54 +01:00
|
|
|
use POSIX qw(tcgetpgrp getpgrp); # to check whether process is in background or foreground
|
2014-03-14 06:51:15 +01:00
|
|
|
use Carp ();
|
|
|
|
|
|
|
|
sub new {
|
2020-01-24 05:47:18 +01:00
|
|
|
Carp::croak("Options to StdinReader should be key/value pairs, not hash reference") if ref($_[1]) eq 'HASH';
|
2014-03-14 06:51:15 +01:00
|
|
|
my ($class, %conf) = @_;
|
|
|
|
my $self = bless {}, $class;
|
|
|
|
$self->initialize(%conf);
|
|
|
|
return $self;
|
|
|
|
}
|
2010-03-17 07:36:54 +01:00
|
|
|
|
2014-03-14 06:51:15 +01:00
|
|
|
sub initialize {
|
|
|
|
my ($self, %conf) = @_;
|
|
|
|
|
2020-01-24 05:47:18 +01:00
|
|
|
$self->{pbot} = $conf{pbot} // Carp::croak("Missing pbot reference in StdinReader");
|
|
|
|
|
|
|
|
# create implicit bot-admin account for bot
|
2020-01-25 21:28:05 +01:00
|
|
|
if (not $self->{pbot}->{users}->find_admin('.*', '*!stdin@pbot')) {
|
|
|
|
my $botnick = $self->{pbot}->{registry}->get_value('irc', 'botnick');
|
2020-01-24 05:47:18 +01:00
|
|
|
$self->{pbot}->{logger}->log("Adding stdin admin *!stdin\@pbot...\n");
|
2020-01-25 21:28:05 +01:00
|
|
|
$self->{pbot}->{users}->add_user($botnick, '.*', '*!stdin@pbot', 100, undef, 1);
|
|
|
|
$self->{pbot}->{users}->login($botnick, "$botnick!stdin\@pbot", undef);
|
|
|
|
$self->{pbot}->{users}->save;
|
2020-01-24 05:47:18 +01:00
|
|
|
}
|
2014-03-14 06:51:15 +01:00
|
|
|
|
|
|
|
# used to check whether process is in background or foreground, for stdin reading
|
|
|
|
open TTY, "</dev/tty" or die $!;
|
|
|
|
$self->{tty_fd} = fileno(TTY);
|
|
|
|
|
|
|
|
$self->{pbot}->{select_handler}->add_reader(\*STDIN, sub { $self->stdin_reader(@_) });
|
|
|
|
}
|
|
|
|
|
|
|
|
sub stdin_reader {
|
|
|
|
my ($self, $input) = @_;
|
2020-01-04 06:22:14 +01:00
|
|
|
chomp $input;
|
2010-03-17 07:36:54 +01:00
|
|
|
|
|
|
|
# make sure we're in the foreground first
|
2014-03-14 06:51:15 +01:00
|
|
|
$self->{foreground} = (tcgetpgrp($self->{tty_fd}) == getpgrp()) ? 1 : 0;
|
|
|
|
return if not $self->{foreground};
|
|
|
|
|
2014-05-18 22:09:05 +02:00
|
|
|
$self->{pbot}->{logger}->log("---------------------------------------------\n");
|
2020-01-24 05:47:18 +01:00
|
|
|
$self->{pbot}->{logger}->log("Got STDIN: $input\n");
|
2014-03-14 06:51:15 +01:00
|
|
|
|
|
|
|
my ($from, $text);
|
|
|
|
|
2019-05-28 18:19:42 +02:00
|
|
|
if ($input =~ m/^~([^ ]+)\s+(.*)/) {
|
2014-03-14 06:51:15 +01:00
|
|
|
$from = $1;
|
2015-04-19 10:53:46 +02:00
|
|
|
$text = $self->{pbot}->{registry}->get_value('irc', 'botnick') . " $2";
|
2014-03-14 06:51:15 +01:00
|
|
|
} else {
|
2020-01-04 05:37:58 +01:00
|
|
|
$from = $self->{pbot}->{registry}->get_value('irc', 'botnick') . "!stdin\@pbot";
|
2015-04-19 10:53:46 +02:00
|
|
|
$text = $self->{pbot}->{registry}->get_value('irc', 'botnick') . " $input";
|
2010-03-17 07:36:54 +01:00
|
|
|
}
|
2014-03-14 06:51:15 +01:00
|
|
|
|
2020-01-04 05:37:58 +01:00
|
|
|
return $self->{pbot}->{interpreter}->process_line($from, $self->{pbot}->{registry}->get_value('irc', 'botnick'), "stdin", "pbot", $text);
|
2010-03-17 07:36:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
1;
|