Daemonized tts process to prevent text blocking.

This commit is contained in:
Ritchie Cunningham 2025-06-07 15:59:29 +01:00
parent a25b527804
commit 44c8e829db

View File

@ -2,7 +2,7 @@ use strict;
use Irssi; use Irssi;
use vars qw($VERSION %IRSSI); use vars qw($VERSION %IRSSI);
$VERSION = "3.0-themeproof"; $VERSION = "3.1-daemonise";
%IRSSI = ( %IRSSI = (
authors => 'Ritchie Cunningham', authors => 'Ritchie Cunningham',
contact => 'ritchie@ritchiecunningham.co.uk', contact => 'ritchie@ritchiecunningham.co.uk',
@ -24,9 +24,25 @@ sub notify {
system("notify-send", "-i", "irssi", $title, $message); system("notify-send", "-i", "irssi", $title, $message);
if (Irssi::settings_get_bool('festival_enabled')) { if (Irssi::settings_get_bool('festival_enabled')) {
my $text = "$title. $message"; my $text_to_speak = "$title, $message";
$text =~ s/[^A-Za-z0-9\s,.]//g; # Sanitize for festival.
system("echo '$text' | festival --tts --language english --pipe &"); # Fork the process to prevent Irssi from freezing while festival speaks.
# :oop: Actually, we're going to have to double-fork to prevent
# festival from changing the parent into some raw mode.
#
# FINE!! Go screw yourself! as neither forking, nor double-forking worked out
# we'll use systemd-run for a guaranteed detachment.
# This will offload the entire backgrounding task to the systemd service
# manager.
# We need to escape any single quotes in the text..
# Sorry @dacav, tried to use your open() suggestion, but as we can't
# daemonise using the fork solution, we have to manually prevent shell injection.
$text_to_speak =~ s/'/'\\''/g;
my $command = "echo '$text_to_speak' | festival --tts --language english --pipe";
system("systemd-run", "--user", "--quiet", "--no-ask-password", "/bin/sh", "-c", $command)
} }
} }