irssi-scripts/fnotify.pl

74 lines
2.4 KiB
Perl

use strict;
use Irssi;
use vars qw($VERSION %IRSSI);
$VERSION = "3.0-themeproof";
%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 = "$title. $message";
$text =~ s/[^A-Za-z0-9\s,.]//g; # Sanitize for festival.
system("echo '$text' | festival --tts --language english --pipe &");
}
}
# --- 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');