[Add] Audio alert for incoming messages. FU TTS.

This commit is contained in:
Ritchie Cunningham 2025-06-15 14:27:14 +01:00
parent 3c48e91174
commit 6113da6f59
2 changed files with 41 additions and 13 deletions

View File

@ -1,28 +1,62 @@
use strict; use strict;
use Irssi; use Irssi;
use vars qw($VERSION %IRSSI); use vars qw($VERSION %IRSSI);
use FindBin;
use POSIX;
$VERSION = "3.1-daemonise"; $VERSION = "4.0";
%IRSSI = ( %IRSSI = (
authors => 'Ritchie Cunningham', authors => 'Ritchie Cunningham',
contact => 'ritchie@ritchiecunningham.co.uk', contact => 'ritchie@ritchiecunningham.co.uk',
name => 'fnotify', name => 'fnotify',
description => 'Sends notify-send notifications for private messages and highlights. Optionally uses festival for voice.', description => 'Sends desktop and audio notifications for private messages and highlights.',
license => 'Public Domain', license => 'Public Domain',
); );
# --- Global variables to hold raw message data between signals. --- # --- Global variables to hold raw message data between signals. ---
my ($last_public_msg, $last_public_nick, $last_public_target); my ($last_public_msg, $last_public_nick, $last_public_target);
# --- Configuration. --- # --- Configuration. ---
my $festival_voice = '(voice_us1_mbrola)'; my $festival_voice = '(voice_us1_mbrola)';
# --- Core Functions. --- # --- Core Functions. ---
sub notify { sub notify {
my ($title, $message) = @_; my ($title, $message) = @_;
# --- Visual desktop notification. ---
system("notify-send", "-i", "irssi", $title, $message); system("notify-send", "-i", "irssi", $title, $message);
# --- Play a notification sound file. ---
if(Irssi::settings_get_bool('fnotify_sound_enabled')) {
my $sound_player = Irssi::settings_get_str('fnotify_sound_player');
my $sound_file = Irssi::settings_get_str('fnotify_sound_file');
if($sound_player && $sound_file) {
# If the path isn't absolute, prepend the script's directory.
# Note: For best results, set the full absolute path in with /set command.
if($sound_file !~ m#^/#) {
$sound_file = "$FindBin::Bin/$sound_file";
}
# Use a non-blocking fork to play sound without freezing Irssi.
my $pid = fork();
if(!defined $pid) {
Irssi::print("fnotify: Could not fork() to play sound: $!", MSGLEVEL_CLIENTERROR);
return;
}
if($pid == 0) {
# Child process. Detach and execute the sound player.
close STDIN; close STDOUT; close STDERR;
POSIX::setsid();
exec($sound_player, $sound_file);
exit(127); # Kill child if exec fails for any reason.
}
# Aaaand back with the parent.
}
}
# --- TTS using festival. ---
if(Irssi::settings_get_bool('festival_enabled')) { if(Irssi::settings_get_bool('festival_enabled')) {
my $text_to_speak = "$title, $message"; my $text_to_speak = "$title, $message";
@ -49,8 +83,6 @@ sub notify {
} }
# --- Signal Handlers. --- # --- Signal Handlers. ---
# Private messages are simple and clean. No theme conflicts.
sub private_message_handler { sub private_message_handler {
my ($server, $msg, $nick, $address) = @_; my ($server, $msg, $nick, $address) = @_;
return if($nick =~ /^(NickServ|ChanServ|MemoServ)$/i); return if($nick =~ /^(NickServ|ChanServ|MemoServ)$/i);
@ -67,23 +99,19 @@ sub public_message_handler {
sub print_text_handler { sub print_text_handler {
my ($dest, $text, $stripped) = @_; my ($dest, $text, $stripped) = @_;
# Check if the level is a highlight and the target matches the one we just saved.
# The target check prevents us from misfiring on other window text.
if(($dest->{level} & Irssi::MSGLEVEL_HILIGHT) && ($dest->{target} eq $last_public_target)) { if(($dest->{level} & Irssi::MSGLEVEL_HILIGHT) && ($dest->{target} eq $last_public_target)) {
# Make sure the data from the first signal is actually there.
return unless defined $last_public_nick; return unless defined $last_public_nick;
# Use the CLEAN data we saved to build the notification!
notify("Mentioned by $last_public_nick in $last_public_target", $last_public_msg); notify("Mentioned by $last_public_nick in $last_public_target", $last_public_msg);
# Clear the variables so we don't accidentally re-use them.
undef $last_public_nick; undef $last_public_nick;
} }
} }
# --- Settings and Signal Registration. --- # --- Settings and Signal Registration. ---
Irssi::settings_add_bool('lookandfeel', 'festival_enabled', 0); Irssi::settings_add_bool('lookandfeel', 'festival_enabled', 0);
Irssi::settings_add_bool('lookandfeel', 'fnotify_sound_enabled', 1);
Irssi::settings_add_str('lookandfeel', 'fnotify_sound_player', 'mpg123');
Irssi::settings_add_str('lookandfeel', 'fnotify_sound_file', 'msg_notification.mp3');
# Register all three handlers. # Register all three handlers.
Irssi::signal_add('message private', 'private_message_handler'); Irssi::signal_add('message private', 'private_message_handler');

BIN
msg_notification.mp3 Normal file

Binary file not shown.