irssi-autospeak/autospeak.pl

224 lines
7.0 KiB
Perl

#autospeak, autospeak incoming events using espeak-ng
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; version 3 of the License.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABI-
# LITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
# License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
use strict;
use warnings;
use Encode;
use Irssi;
# The character encoding of the Irssi client:
use constant ENCODING => "UTF-8";
# General script information:
our $VERSION = '1.0';
our %IRSSI = (
name => 'autospeak',
description => 'Autospeak incoming messages.',
authors => 'Jeremiah Ticket',
contact => 'seashellpromises@gmail.com',
url => 'https://git.stormux.org/jticket/irssi-autospeak',
license => 'GNU General Public License, version 3',
changed => '2023/10/05',
);
# Speak Some Text
sub speak_message {
my ($summary, $body) = @_;
# Convert the strings to Perl's internal representation:
$summary = decode(ENCODING, $summary);
$body = decode(ENCODING, $body);
my $SpeechRate = Irssi::settings_get_int("speech_rate");
my $SpeechVolume = Irssi::settings_get_int("speech_volume");
my $SpeechVoice = Irssi::settings_get_str("speech_voice");
my $SpeechModule = Irssi::settings_get_str("speech_module");
my $SpeechPriority = Irssi::settings_get_str("speech_priority");
# Speak The Message
my @spd = ("spd-say",
"-P",
$SpeechPriority,
"-o",
$SpeechModule,
"-y",
$SpeechVoice,
"-r",
$SpeechRate,
"-i",
$SpeechVolume,
"$summary $body");
system(@spd);
}
# Handle incoming public messages:
sub message_public {
my ($server, $message, $nick, $address, $target) = @_;
# Check whether to notify the user about public messages:
return unless (Irssi::settings_get_bool('speak_public_messages'));
# Check whether to notify the user about messages in the active window:
unless (Irssi::settings_get_bool('speak_active_window')) {
# Get the name of the active window:
my $window = Irssi::active_win()->{active}->{name} || '';
# Ignore messages in the active window:
return if ($window eq $target);
}
# Get the user's nick name:
my $user = $server->{nick};
#check to notify about all messages:
if ((Irssi::settings_get_bool('speak_all_public_messages')) == 1) {
# Get the server's tag:
my $tag = $server->{tag};
# Prepare the message body:
(my $body = $message) =~ s/^$user[\s:,]\s*//;
# Notify the user about the incoming public message:
speak_message("Message from $nick/$tag on $target:", $body);
return;
}
# Check whether to notify the user about indirect messages:
elsif ((Irssi::settings_get_bool('speak_indirect_messages')) == 0) {
# Ignore messages that are not explicitly addressed to the user:
return if ($message !~ /^$user[\s:,]/);
}
else {
# Ignore messages that do not mention the user:
return if ($message !~ /\b$user\b/);
}
# Get the server's tag:
my $tag = $server->{tag};
# Prepare the message body:
(my $body = $message) =~ s/^$user[\s:,]\s*//;
# Notify the user about the incoming public message:
speak_message("Message from $nick/$tag on $target:", $body);
}
# Handle incoming private messages:
sub message_private {
my ($server, $message, $nick, $address) = @_;
# Check whether to notify the user about private messages:
return unless (Irssi::settings_get_bool('speak_private_messages'));
# Check whether to notify the user about messages in the active window:
unless (Irssi::settings_get_bool('speak_active_window')) {
# Get the name of the active window:
my $window = Irssi::active_win()->{active}->{name} || '';
# Ignore messages in the active window:
return if ($window eq $nick);
}
# Get the server's tag:
my $tag = $server->{tag};
# Notify the user about the incoming private message:
speak_message("Message from $nick/$tag:", $message);
}
# Handle incoming DCC requests:
sub dcc_request {
my ($dcc, $sendaddr) = @_;
# Check whether to notify the user about DCC requests:
return unless (Irssi::settings_get_bool('speak_dcc_messages'));
# Check whether to notify the user about messages in the active window:
unless (Irssi::settings_get_bool('speak_active_window')) {
# Get the name of the active window:
my $window = Irssi::active_win()->{active}->{name} || '';
# Ignore messages in the active window:
return unless ($window);
}
# Get the request type:
my $type = $dcc->{type};
# Get the sender's nick:
my $nick = $dcc->{nick};
# Get the server's tag:
my $tag = $dcc->{server}->{tag};
# Check the request type:
if ($type eq 'GET') {
# Get the file name and size:
my $name = $dcc->{arg};
my $size = $dcc->{size};
# Notify the user about the incoming SEND request:
speak_message("$nick/$tag offers a file:", "$name ($size B)");
}
elsif ($type eq 'CHAT') {
# Notify the user about the incoming CHAT request:
speak_message("$nick/$tag offers a DCC chat.", "");
}
}
# Handle incoming DCC CHAT messages:
sub dcc_chat_message {
my ($dcc, $message) = @_;
# Check whether to notify the user about DCC requests:
return unless (Irssi::settings_get_bool('speak_dcc_messages'));
# Get the sender's nick:
my $nick = $dcc->{id};
# Check whether to notify the user about messages in the active window:
unless (Irssi::settings_get_bool('speak_active_window')) {
# Get the name of the active window:
my $window = Irssi::active_win()->{active}->{name} || '';
# Ignore messages in the active window:
return if ($window eq "=$nick");
}
# Get the server's tag:
my $tag = $dcc->{server}->{tag};
# Notify the user about the incoming CHAT message:
speak_message("DCC chat message from $nick/$tag:", $message);
}
# Register configuration options:
Irssi::settings_add_int("autospeak", "speech_rate", 0);
Irssi::settings_add_int("autospeak", "speech_volume", 0);
Irssi::settings_add_str("autospeak", "speech_voice", "en-us");
Irssi::settings_add_str("autospeak", "speech_module", "espeak-ng");
Irssi::settings_add_str("autospeak", "speech_priority", "text");
Irssi::settings_add_bool('autospeak', 'speak_private_messages', 1);
Irssi::settings_add_bool('autospeak', 'speak_public_messages', 1);
Irssi::settings_add_bool('autospeak', 'speak_all_public_messages', 0);
Irssi::settings_add_bool('autospeak', 'speak_indirect_messages',0);
Irssi::settings_add_bool('autospeak', 'speak_active_window', 0);
Irssi::settings_add_bool('autospeak', 'speak_dcc_messages', 1);
# Register signals:
Irssi::signal_add('message public', 'message_public');
Irssi::signal_add('message private', 'message_private');
Irssi::signal_add('dcc request', 'dcc_request');
Irssi::signal_add('dcc chat message', 'dcc_chat_message');