From ded8d315fd3f6c050f759865b09f053ae85e52a5 Mon Sep 17 00:00:00 2001 From: Andrew DeFaria Date: Wed, 24 Feb 2021 11:47:38 -0800 Subject: [PATCH] Moved Say -> Speak::speak --- bin/announceEmail.pl | 28 ++---- bin/tunnel.pl | 22 +---- lib/Speak.pm | 203 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 212 insertions(+), 41 deletions(-) create mode 100644 lib/Speak.pm diff --git a/bin/announceEmail.pl b/bin/announceEmail.pl index 7a76fc4..f90426e 100755 --- a/bin/announceEmail.pl +++ b/bin/announceEmail.pl @@ -73,8 +73,9 @@ use lib "$FindBin::Bin/../lib"; use Display; use Logger; -use Utils; +use Speak; use TimeUtils; +use Utils; my $defaultIMAPServer = 'defaria.com'; my $IMAP; @@ -152,23 +153,6 @@ sub Connect2IMAP() { return; } # Connect2IMAP -sub Say($) { - my ($msg) = @_; - - if (-f "$FindBin::Bin/shh") { - $log->msg("Not speaking because we were asked to be quiet - $msg"); - - return; - } # if - - my ($status, @output) = Execute "/usr/local/bin/gt \"$msg\""; - - $log->err("Unable to speak (Status: $status) - " - . join ("\n", @output), $status) if $status; - - return; -} # Say - sub MonitorMail() { MONITORMAIL: @@ -224,8 +208,8 @@ sub MonitorMail() { # Only announce if after 6 Am. Note this will announce up until # midnight but that's ok. I want midnight to 6 Am as silent time. - if ($hour >= 6) { - Say $msg; + if ($hour >= 7) { + speak $msg, $log; $log->msg($logmsg); } else { $log->msg("$logmsg [silent]"); @@ -267,7 +251,7 @@ END { if ($log) { my $msg = "$FindBin::Script ending unexpectedly!"; - Say $msg; + speak $msg, $log; $log->err($msg); } # if @@ -324,7 +308,7 @@ if ($opts{username} =~ /(.*)\@/) { my $msg = "Now monitoring email for $opts{user}\@$opts{name}"; -Say $msg if $opts{announce}; +speak $msg, $log if $opts{announce}; $log->msg($msg); diff --git a/bin/tunnel.pl b/bin/tunnel.pl index 56be859..86cf076 100755 --- a/bin/tunnel.pl +++ b/bin/tunnel.pl @@ -68,6 +68,7 @@ use Pod::Usage; use Display; use Logger; +use Speak; use Utils; my $VERSION = '$Revision: 1.0 $'; @@ -89,27 +90,10 @@ my %opts = ( my ($log, $ssh); -sub Say($) { - my ($msg) = @_; - - if (-f "$FindBin::Bin/shh") { - $log->msg("Not speaking because we were asked to be quiet - $msg"); - - return; - } # if - - my ($status, @output) = Execute "/usr/local/bin/gt \"$msg\""; - - $log->err("Unable to speak (Status: $status) - " - . join ("\n", @output), $status) if $status; - - return; -} # Say - sub Report ($;$) { my ($msg, $err) = @_; - Say $msg; + speak $msg, $log; if ($err) { $log->err($msg, $err); @@ -157,7 +141,7 @@ RETRY: my $msg = 'Ssh tunnel '; $msg .= $retryattempts ? 'reestablished' : 'established'; - Say $msg if $opts{announce}; + speak $msg, $log if $opts{announce}; $log->msg($msg); diff --git a/lib/Speak.pm b/lib/Speak.pm new file mode 100644 index 0000000..0869ed4 --- /dev/null +++ b/lib/Speak.pm @@ -0,0 +1,203 @@ +=pod + +=head1 NAME $RCSfile: Speak.pm,v $ + +Convert text to speach using Google's engine and play it on speakers + +=head1 VERSION + +=over + +=item Author + +Andrew DeFaria + +=item Revision + +$Revision: 1.0 $ + +=item Created + +Wed 24 Feb 2021 11:05:36 AM PST + +=item Modified + + +=back + +=head1 SYNOPSIS + +This module offers subroutines to convert text into speach and speak them. + +=head2 DESCRIPTION + +This module exports subroutines to process text to speach and speak them. + +=head1 ROUTINES + +The following routines are exported: + +=cut + +package Speak; + +use strict; +use warnings; + +use base 'Exporter'; + +use FindBin; + +use lib "$FindBin::Bin/../lib"; + +use Display; +use Logger; +use Utils; + +our @EXPORT = qw(speak); + +sub speak (;$$) { + my ($msg, $log) = @_; + +=pod + +=head2 speak($msg, $log) + +Convert $msg to speach. + +Note this currently uses an external script to do the conversion. I intend to +re-write that into Perl here eventually. + +Parameters: + +=for html
+ +=over + +=item $msg: + +Message to speak. If $msg is defined and scalar then that is the message +to speak. If it is a file handle then the text will be read from that file. +Otherwise the text in the clipboard will be used. + +=item $log + +If provided, errors and messages will be logged to the logfile, otherwise stdout + +=back + +=for html
+ +Returns: + +=for html
+ +=over + +=item Nothing + +=back + +=for html
+ +=cut + + if (-f "$FindBin::Bin/shh") { + if ($log) { + $log->msg("Not speaking because we were asked to be quiet - $msg"); + } else { + verbose "Not speaking because we were asked to be quiet - $msg"; + } # if + + return; + } # if + + # Handle the case where $msg is a filehandle + $msg = <$msg> if ref $msg eq 'GLOB'; + + # We can't have two speakers going at the same time so if we have an error + # backoff a little and try again. + my $attempts = 0; + my $maxretries = 3; + my $backoff = 2; + + my ($status, @output); + + while ($attempts++ < $maxretries) { + ($status, @output) = Execute "/usr/local/bin/gt \"$msg\""; + + if ($status) { + my $errmsg = "Unable to speak (Status: $status) - " . join "\n", @output; + + if ($log) { + $log->err($errmsg); + } else { + error $errmsg; + } # if + + sleep $backoff++; + } else { + return; # We said our piece... + } # if + } # while + + my $errmsg = 'Maximum retries exceeded - terminating'; + + if ($log) { + $log->err($errmsg, $status); + } else { + error $errmsg, $status; + } # if + + return; +} # speak + +1; + +=pod + +=head1 CONFIGURATION AND ENVIRONMENT + +DEBUG: If set then $debug is set to this level. + +VERBOSE: If set then $verbose is set to this level. + +TRACE: If set then $trace is set to this level. + +=head1 DEPENDENCIES + +=head2 Perl Modules + +L + +L + +=head1 INCOMPATABILITIES + +None yet... + +=head1 BUGS AND LIMITATIONS + +There are no known bugs in this module. + +Please report problems to Andrew DeFaria . + +=head1 LICENSE AND COPYRIGHT + +This Perl Module is freely available; you can redistribute it and/or +modify it under the terms of the GNU General Public License as +published by the Free Software Foundation; either version 2 of the +License, or (at your option) any later version. + +This Perl Module is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License (L) for more +details. + +You should have received a copy of the GNU General Public License +along with this Perl Module; if not, write to the Free Software Foundation, +Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +reserved. + +=cut -- 2.17.1