use strict; use Irssi; use vars qw($VERSION %IRSSI); $VERSION = "3.1-daemonise"; %IRSSI = ( authors => 'Ritchie Cunningham', contact => 'ritchie@ritchiecunningham.co.uk', name => 'fnotify', description => 'Sends notify-send notifications for private messages and highlights. Optionally uses festival for voice.', license => 'Public Domain', ); # --- Global variables to hold raw message data between signals. --- my ($last_public_msg, $last_public_nick, $last_public_target); # --- Configuration. --- my $festival_voice = '(voice_us1_mbrola)'; # --- Core Functions. --- sub notify { my ($title, $message) = @_; system("notify-send", "-i", "irssi", $title, $message); if(Irssi::settings_get_bool('festival_enabled')) { my $text_to_speak = "$title, $message"; # 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. # 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; # Must escape backslashes first $text_to_speak =~ s/"/\\"/g; # Then escape double-quotes my $scheme_command = "$festival_voice(SayText \"$text_to_speak\")"; $scheme_command =~ s/'/'\\''/g; my $command = "echo '$scheme_command' | festival"; system("systemd-run", "--user", "--quiet", "--no-ask-password", "/bin/sh", "-c", $command); } } # --- Signal Handlers. --- # Private messages are simple and clean. No theme conflicts. sub private_message_handler { my ($server, $msg, $nick, $address) = @_; return if($nick =~ /^(NickServ|ChanServ|MemoServ)$/i); notify("Private Message from $nick", $msg); } # FFS! THEME! Stop f.cking with my data please.. sub public_message_handler { my ($server, $msg, $nick, $address, $target) = @_; $last_public_msg = $msg; $last_public_nick = $nick; $last_public_target = $target; } sub print_text_handler { 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)) { # Make sure the data from the first signal is actually there. 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); # Clear the variables so we don't accidentally re-use them. undef $last_public_nick; } } # --- Settings and Signal Registration. --- Irssi::settings_add_bool('lookandfeel', 'festival_enabled', 0); # Register all three handlers. Irssi::signal_add('message private', 'private_message_handler'); Irssi::signal_add('message public', 'public_message_handler'); Irssi::signal_add('print text', 'print_text_handler');