App-KGB

 view release on metacpan or  search on metacpan

script/kgb-bot  view on Meta::CPAN

use Getopt::Long;
use YAML ();
use Proc::PID::File;

KGB::save_progname();
$KGB::out = \*STDERR;
binmode( $KGB::out, ':utf8' );

my $conf_file  = '/etc/kgb-bot/kgb.conf';
my $conf_dir   = '/etc/kgb-bot/kgb.conf.d';
$KGB::foreground = 0;
$KGB::simulate = 0;
$KGB::simulate_color = 0;
$KGB::debug = 0;

Getopt::Long::Configure("bundling");
GetOptions(
    'c|config=s'   => \$conf_file,
    'cd|config-dir=s' => \$conf_dir,
    'f|foreground' => \$KGB::foreground,
    'simulate=s'    => \$KGB::simulate,
    'simulate-color!' => \$KGB::simulate_color,
    'debug!'        => \$KGB::debug,
) or die 'Invalid parameters';

@ARGV and die "No command line arguments supported\n";

KGB::load_conf($conf_file);

use Cwd;
$KGB::simulate = Cwd::realpath($KGB::simulate) if $KGB::simulate;

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

our $pid_keeper;

unless ($KGB::foreground) {
    pipe IN, OUT or die "pipe: $!\n";
    my $pid = fork();
    die "Can't fork: $!" unless ( defined $pid );
    if ($pid) {
        close OUT;
        my $r = join( "", <IN> );
        close IN or die $!;
        if ( $r =~ /^OK$/ ) {
            exit 0;
        }
        else {
            die $r;
        }

        die "Should not happen";
    }

    $poe_kernel->has_forked;

    close IN;
    eval {
        $pid_keeper = Proc::PID::File->new(
            verify => 1,
            dir    => $KGB::config->{pid_dir},
        );
        die "Already running\n" if $pid_keeper->alive;
        $pid_keeper->write;
        POSIX::setsid() or die "setsid: $!\n";
        umask(0022);
        chdir("/") or die "chdir: $!\n";

        open( STDIN, "<", "/dev/null" ) or die "Error closing stdin: $!\n";

        KGB::open_log();
    };
    if ($@) {
        print OUT $@;
        exit 1;
    }
    else {
        print OUT "OK\n";
        close OUT;
    }
}

POE::Component::Server::SOAP->new(
    ALIAS   => $KGB::const{SOAPsvc},
    ADDRESS => $KGB::config->{soap}{server_addr},
    PORT    => $KGB::config->{soap}{server_port},
    # override PoCo::SOAP HANDLERS to plug json-rpc
    SIMPLEHTTP => {
                'HANDLERS'      =>      [
                        {
                                'DIR'           =>      '^/json-rpc',
                                'SESSION'       =>      $KGB::config->{soap}{service_name},
                                'EVENT'         =>      'json_request',
                        },
                        {
                                'DIR'           =>      '^/webhook/',
                                'SESSION'       =>      $KGB::config->{soap}{service_name},
                                'EVENT'         =>      'webhook_request',
                        },
                        {
                                'DIR'           =>      '.*',
                                'SESSION'       =>      'SOAPServer',
                                'EVENT'         =>      'Got_Request',
                        },
                ],
    },
);

POE::Session->create(
    package_states => [
        "KGB::POE" => [
            qw(_start _stop sighandler restarthandler
                reloadhandler)
        ],
        "KGB::IRC" => [
            qw(_irc_reconnect irc_registered irc_001
                irc_public irc_bot_addressed irc_new_hash irc_notify _default
                irc_command irc_msg
                irc_chan_sync
            ),



( run in 0.353 second using v1.01-cache-2.11-cpan-d7f47b0818f )