App-KGB

 view release on metacpan or  search on metacpan

script/kgb-bot  view on Meta::CPAN

    $conf->{colors}{failure}     //= 'reverse red';

    $KGB::debug = $conf->{debug} if exists $conf->{debug};
    $conf->{pid_dir}
        = Cwd::realpath( $conf->{pid_dir} // '/var/run/kgb-bot' );

    if (    exists $conf->{webhook}
        and exists $conf->{webhook}{allowed_networks} )
    {
        $_ = Net::IP->new($_) for @{ $conf->{webhook}{allowed_networks} };
    }

    KGB->debug( JSON::XS->new->convert_blessed(1)->encode($conf) );
    return $conf;
}

sub load_conf($) {
    my $file = shift;
    my $conf = read_conf($file);

    # Save globals
    $config_file = Cwd::realpath($file);
    $config      = $conf;

    return $conf;
}

sub reload_conf() {
    my $new_conf = eval { KGB::read_conf($config_file) };
    if ($@) {
        KGB->out("Error in configuration file: $@");
        return -1;
    }
    if (   $new_conf->{soap}{service_name} ne $config->{soap}{service_name}
        or $new_conf->{soap}{server_port} ne $config->{soap}{server_port}
        or $new_conf->{soap}{server_addr} ne $config->{soap}{server_addr} )
    {
        KGB->out("Cannot reload configuration file, restarting\n");
        return -2;    # need restart
    }

    $painter =
        App::KGB::Painter->new( { item_colors => $new_conf->{colors} } );

    KGB->out("Configuration file reloaded\n");
    $config = $new_conf;
    return 0;
}

sub out {
    shift;
    print $KGB::out localtime->strftime('%Y.%m.%d %H:%M:%S').': ', @_, ( $_[-1] =~ /\n$/s ) ? () : "\n";
}

sub debug {
    return unless $KGB::debug;

    my $self = shift;

    my $first_line = shift;
    $first_line = (caller)[2] . ': ' . $first_line;

    $self->out( $first_line, @_ );
}

sub open_log {
    if ( my $f = $KGB::config->{log_file} ) {
        open( STDOUT, ">>", $f )
            or die "Error opening log $f: $!\n";
        open( STDERR, ">>", $f )
            or die "Error opening log $f: $!\n";
    }
    else {
        open( STDOUT, ">", "/dev/null" )
            or die "Error closing stdout: $!\n";
        open( STDERR, ">", "/dev/null" )
            or die "Error closing stderr: $!\n";
    }
}

sub count {
    my ($self, $counter) = @_;

    $stats{$counter}++;
}

sub get_stats {
    return \%stats;
}

sub get_run_seconds {
    return time - $run_time_since;
}

sub reset_stats {
    %stats = ();
    $run_time_since = time;
}

sub get_chanid ($$) {
    my $self = shift;
    my $net = shift // "";
    my $chan = shift // "";
    return "$net/$chan";
}

sub get_net_chan ($) {
    my $self = shift;
    my $chanid = shift;
    die "Invalid chanid $chanid"
        unless exists $KGB::config->{chanidx}{$chanid};
    return ($KGB::config->{chanidx}{$chanid}{network},
        $KGB::config->{chanidx}{$chanid}{name});
}

1;

package KGB::POE;

use strict;
use warnings;



( run in 1.305 second using v1.01-cache-2.11-cpan-ff9377addf4 )