| 1 |
#!/usr/bin/perl |
|---|
| 2 |
use warnings; |
|---|
| 3 |
use strict; |
|---|
| 4 |
|
|---|
| 5 |
use FindBin; |
|---|
| 6 |
use lib "$FindBin::Bin/../lib"; |
|---|
| 7 |
|
|---|
| 8 |
use Getopt::Long; |
|---|
| 9 |
use List::Util qw(first); |
|---|
| 10 |
use YAML; |
|---|
| 11 |
|
|---|
| 12 |
use Plagger::ConfigLoader; |
|---|
| 13 |
|
|---|
| 14 |
use POE qw( |
|---|
| 15 |
Session |
|---|
| 16 |
Component::Client::Lingr |
|---|
| 17 |
Component::IKC::Server |
|---|
| 18 |
Component::IKC::Specifier |
|---|
| 19 |
); |
|---|
| 20 |
|
|---|
| 21 |
sub msg (@) { print "[msg] ", "@_\n" } |
|---|
| 22 |
sub err (@) { print "[err] ", "@_\n" } |
|---|
| 23 |
|
|---|
| 24 |
my $path = "$FindBin::Bin/../config.yaml"; |
|---|
| 25 |
GetOptions("--config=s", \$path); |
|---|
| 26 |
Getopt::Long::Configure("bundling"); # allows -c |
|---|
| 27 |
|
|---|
| 28 |
msg "loading configuration $path"; |
|---|
| 29 |
|
|---|
| 30 |
my $loader = Plagger::ConfigLoader->new; |
|---|
| 31 |
my $base_config = $loader->load($path); |
|---|
| 32 |
|
|---|
| 33 |
$loader->load_include($base_config); |
|---|
| 34 |
$loader->load_recipes($base_config); |
|---|
| 35 |
|
|---|
| 36 |
my $plugin = first { $_->{module} eq 'Notify::Lingr' } @{ $base_config->{plugins} } |
|---|
| 37 |
or die "Can't find Notify::Lingr config in $path"; |
|---|
| 38 |
|
|---|
| 39 |
my $config = $plugin->{config}; |
|---|
| 40 |
$config->{api_key} or die "config 'api_key' is required"; |
|---|
| 41 |
$config->{room} or die "config 'room' is required"; |
|---|
| 42 |
|
|---|
| 43 |
$config->{nickname} ||= 'plaggerbot'; |
|---|
| 44 |
|
|---|
| 45 |
msg 'creating daemon component'; |
|---|
| 46 |
POE::Component::IKC::Server->spawn( |
|---|
| 47 |
port => $config->{daemon_port} || 9999, |
|---|
| 48 |
name => 'NotifyLingrBot', |
|---|
| 49 |
); |
|---|
| 50 |
|
|---|
| 51 |
msg 'creating Lingr component'; |
|---|
| 52 |
POE::Component::Client::Lingr->spawn( alias => 'bot' ); |
|---|
| 53 |
|
|---|
| 54 |
msg 'creating kernel session'; |
|---|
| 55 |
POE::Session->create( |
|---|
| 56 |
inline_states => { |
|---|
| 57 |
_start => \&bot_start, |
|---|
| 58 |
_stop => \&bot_stop, |
|---|
| 59 |
update => \&update, |
|---|
| 60 |
connect => \&bot_connect, |
|---|
| 61 |
'lingr.session.create' => \&lingr_session_create, |
|---|
| 62 |
} |
|---|
| 63 |
); |
|---|
| 64 |
|
|---|
| 65 |
msg 'starting the kernel'; |
|---|
| 66 |
POE::Kernel->run(); |
|---|
| 67 |
msg 'exiting'; |
|---|
| 68 |
exit 0; |
|---|
| 69 |
|
|---|
| 70 |
sub update |
|---|
| 71 |
{ |
|---|
| 72 |
my ( $kernel, $heap, $msg ) = @_[ KERNEL, HEAP, ARG0 ]; |
|---|
| 73 |
eval { |
|---|
| 74 |
$kernel->post(bot => call => 'room.say', { message => $msg }); |
|---|
| 75 |
}; |
|---|
| 76 |
err "update error: $@" if $@; |
|---|
| 77 |
} |
|---|
| 78 |
|
|---|
| 79 |
sub bot_start |
|---|
| 80 |
{ |
|---|
| 81 |
my ( $kernel, $heap ) = @_[ KERNEL, HEAP ]; |
|---|
| 82 |
msg "starting Lingr session"; |
|---|
| 83 |
$kernel->alias_set('notify_lingr'); |
|---|
| 84 |
$kernel->call( IKC => publish => notify_lingr => ['update'] ); |
|---|
| 85 |
$kernel->post( bot => 'register' ); |
|---|
| 86 |
$kernel->yield('connect'); |
|---|
| 87 |
} |
|---|
| 88 |
|
|---|
| 89 |
sub bot_connect |
|---|
| 90 |
{ |
|---|
| 91 |
my ( $kernel, $heap ) = @_[ KERNEL, HEAP ]; |
|---|
| 92 |
msg "attempting to connect to Lingr"; |
|---|
| 93 |
$kernel->post( bot => call => 'session.create', { api_key => $config->{api_key} } ); |
|---|
| 94 |
} |
|---|
| 95 |
|
|---|
| 96 |
sub lingr_session_create { |
|---|
| 97 |
my($kernel, $event) = @_[KERNEL, ARG0]; |
|---|
| 98 |
$kernel->call(bot => call => 'room.enter' => { id => $config->{room}, nickname => $config->{nickname} }); |
|---|
| 99 |
} |
|---|
| 100 |
|
|---|
| 101 |
sub bot_stop |
|---|
| 102 |
{ |
|---|
| 103 |
msg "stopping bot"; |
|---|
| 104 |
} |
|---|