From 7627e48936611b4266f0bd2bd0aa4add6dd143f5 Mon Sep 17 00:00:00 2001 From: Alexandre Morignot <amorignot@meilleursagents.com> Date: Tue, 1 Aug 2023 14:44:11 +0200 Subject: [PATCH] feat(irc): delegate message parsing to external process If the process fails, it goes through the legacy path. --- PlayBot/sessions/irc.pm | 50 ++++++++++++++++++++++++++++++++--------- 1 file changed, 40 insertions(+), 10 deletions(-) diff --git a/PlayBot/sessions/irc.pm b/PlayBot/sessions/irc.pm index c0f9b53..1df7d1f 100644 --- a/PlayBot/sessions/irc.pm +++ b/PlayBot/sessions/irc.pm @@ -256,6 +256,29 @@ sub on_invite { push @channels, $chan; } +sub external_parse { + my ( $user, $chan, $msg ) = shift; + + my $content = `./PlayBot-cli "$user" "$chan" "$msg"`; + if ( $? >> 8 != 0 ) { + die "Error"; + } + + my ( $id, $external_id, $url, $title, $author, $duration ) = + split( /\n/, $content ); + + return ( + id => $id, + author => $author, + external_id => $external_id, + duration => $duration, + playlist => 0, + site => 'soundcloud', + title => $title, + url => $url, + ); +} + # Quand un user parle sub on_speak { my ( $kernel, $user, $chan, $msg ) = @_[ KERNEL, ARG0, ARG1, ARG2 ]; @@ -264,18 +287,25 @@ sub on_speak { my ( $nick, $mask ) = split( /!/, $user ); my %content; - # first we check for url - PlayBot::sites::parse(@args); - - # and we check for command - my $cmd = ( split /\s/, $msg )[0]; - if ( $cmd eq "!fav" ) { - @args = ( $kernel, $user, lc $chan->[0], $cmd ); + eval { + %content = external_parse($user, lc $chan->[0], $msg); + $irc->yield( + privmsg => $chan => PlayBot::utils::print::print($content) ); } - else { - @args = ( $kernel, $user, lc $chan->[0], $msg ); + if ($@) { + # first we check for url + PlayBot::sites::parse(@args); + + # and we check for command + my $cmd = ( split /\s/, $msg )[0]; + if ( $cmd eq "!fav" ) { + @args = ( $kernel, $user, lc $chan->[0], $cmd ); + } + else { + @args = ( $kernel, $user, lc $chan->[0], $msg ); + } + PlayBot::commands::parser::exec(@args); } - PlayBot::commands::parser::exec(@args); } 1; -- GitLab