diff --git a/PlayBot.pl b/PlayBot.pl index 267a126cb793dc6ad1440efa5ff14c0e79d763fc..56d84fd3f2ed942c8f8cf1f782e801c88a6d1144 100755 --- a/PlayBot.pl +++ b/PlayBot.pl @@ -7,6 +7,7 @@ use FindBin; use lib "$FindBin::Bin/lib/"; use sessions::irc; +use sessions::facebook; # Boucle des events POE::Kernel->run(); diff --git a/lib/sessions/facebook.pm b/lib/sessions/facebook.pm new file mode 100644 index 0000000000000000000000000000000000000000..fb646c241e55f970e520647bf08016c5c74800d2 --- /dev/null +++ b/lib/sessions/facebook.pm @@ -0,0 +1,45 @@ +package sessions::facebook; + +# This session have only one event to receive a message from the Facebook +# group. The args are, in order : +# - the sender +# - the msg (wich should contain a link, but is not mandatory) + +use strict; +use warnings; + +use POE; +use POE::Component::IKC::Server; +use FindBin; + +use lib "$FindBin::Bin/lib/"; +use utils::Logging; + +my $log = Logging->new('STDOUT', 1); + +POE::Component::IKC::Server->spawn( + unix => 'playbot.sock', + name => 'PlayBot' +); + +POE::Session->create( + inline_states => { + _start => \&on_start, + fbmsg => \&on_fbmsg + } +); + +sub on_start { + my $kernel = $_[KERNEL]; + $kernel->alias_set('fbrecv'); + $kernel->post(IKC => publish => 'fbrecv', ['fbmsg']); + + $log->info('listening for clients'); +} + +sub on_fbmsg { + my ($kernel, $args) = @_[KERNEL, ARG0]; + $kernel->post('bot' => 'irc_public' => $args->[0], ['#nightiies.facebook'], $args->[1]); +} + +1; diff --git a/tools/readjson.pl b/tools/readjson.pl index 524e7c454b1b6d62ab41bea8eba0922385f4e1a3..3ea35ea1848bab10dfab056b16a9893a77cb6684 100755 --- a/tools/readjson.pl +++ b/tools/readjson.pl @@ -3,23 +3,40 @@ use strict; use JSON; +use POE; +use POE::Component::IKC::Client; use FindBin; -use lib "$FindBin::Bin/lib"; -use commands::parser; -use sites::parser; -use utils::db; -use utils::Logging; - -my $dbh = utils::db::get_session; -my $log = Logging->new('STDOUT', 1); -$sites::parser::dbh = $dbh; -$sites::parser::log = $log; - -foreach (<>) { - my $content = decode_json $_; - foreach (keys %{$content}) { - my $id = sites::parser::parse(undef, $content->{'author'}, ['#nightiies.facebook'], $content->{'link'}); - commands::parser::tag($content->{'msg'}, ['#nightiies.facebook']); - } +POE::Component::IKC::Client->spawn( + unix => "$FindBin::Bin/../playbot.sock", + name => "client-$$", + on_connect => \&on_connect +); + +POE::Component::IKC::Responder->spawn(); +POE::Kernel->run(); +exit; + +sub on_connect { + POE::Session->create( + inline_states => { + _start => \&on_start, + send => \&on_send + } + ); +} + +sub on_start { + print "start\n"; + my $kernel = $_[KERNEL]; + + # we 'slurp' stdin + local $/; + my $json = <>; + my $content = decode_json $json; + print "$json\n"; + + # TODO: handle $content->{'msg'} + $kernel->post('IKC', 'post', 'poe://PlayBot/fbrecv/fbmsg', [ $content->{'author'}, $content->{'link'} ]); + $kernel->post('IKC', 'shutdown'); }