X-Git-Url: https://defaria.com/gitweb/?a=blobdiff_plain;f=bin%2FannounceEmail.pl;h=2826ed37926704919fe858819ba0401e9432f3fd;hb=30994e573e3b0f694dabe361c844cb44c019ca6a;hp=94789e84d27a695ecead72c234fb7043e99c1ef8;hpb=30b20fd03559202343ac8de28938e33866b38c3f;p=clearscm.git diff --git a/bin/announceEmail.pl b/bin/announceEmail.pl index 94789e8..2826ed3 100755 --- a/bin/announceEmail.pl +++ b/bin/announceEmail.pl @@ -31,23 +31,28 @@ $Date: 2019/04/04 13:40:10 $ =head1 SYNOPSIS - Usage: announceEmail.pl [-usa|ge] [-h|elp] [-v|erbose] [-de|bug] [-da|emon] + Usage: announceEmail.pl [-usa|ge] [-h|elp] [-v|erbose] [-de|bug] [-use|rname ] [-p|assword ] - [-i|map ] + [-an|nouce] [-ap|pend] [-da|emon] [-n|name ] + [-uses|sl] [-useb|locking] Where: - -usa|ge: Print this usage - -h|elp: Detailed help - -v|erbose: Verbose mode (Default: -verbose) - -de|bug: Turn on debugging (Default: Off) - -da|emon: Run in daemon mode (Default: -daemon) - -user|name: User name to log in with (Default: $USER) - -p|assword: Password to use (Default: prompted) - -n|ame: Name of account (Default: imap) - -i|map: IMAP server to talk to (Default: defaria.com) - -uses|sl: Whether or not to use SSL to connect (Default: False) + -usa|ge Print this usage + -h|elp Detailed help + -v|erbose Verbose mode (Default: -verbose) + -de|bug Turn on debugging (Default: Off) + + -user|name User name to log in with (Default: $USER) + -p|assword Password to use (Default: prompted) + -i|map IMAP server to talk to (Default: defaria.com) + + -an|nounce Announce startup (Default: False) + -ap|pend Append to logfile (Default: Noappend) + -da|emon Run in daemon mode (Default: -daemon) + -n|ame Name of account (Default: imap) + -uses|sl Whether or not to use SSL to connect (Default: False) -useb|locking Whether to block on socket (Default: False) - -a-nnounce Announce startup (Default: False) =head1 DESCRIPTION @@ -73,8 +78,9 @@ use lib "$FindBin::Bin/../lib"; use Display; use Logger; -use Utils; +use Speak; use TimeUtils; +use Utils; my $defaultIMAPServer = 'defaria.com'; my $IMAP; @@ -103,9 +109,6 @@ my %opts = ( username => $ENV{USER}, password => $ENV{PASSWORD}, imap => $defaultIMAPServer, - usessl => 0, - useblocking => 0, - announce => 0, ); sub interrupted { @@ -152,31 +155,23 @@ sub Connect2IMAP() { return; } # Connect2IMAP -sub Say($) { - my ($msg) = @_; - - 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: + $log->dbug("Top of MonitorMail loop"); # First close and reselect the INBOX to get its current status $IMAP->close; $IMAP->select('INBOX') or $log->err("Unable to select INBOX - ". $IMAP->errstr(), 1); + $log->dbug("Closed and reselected INBOX"); # Go through all of the unseen messages and add them to %unseen if they were # not there already from a prior run and read my %newUnseen = unseenMsgs; # Now clean out any messages in %unseen that were not in the %newUnseen and # marked as previously read + $log->dbug("Cleaning out unseen"); for (keys %unseen) { if (defined $newUnseen{$_}) { if ($unseen{$_}) { @@ -187,6 +182,7 @@ sub MonitorMail() { } # if } # for + $log->dbug("Processing new unseen messages"); for (keys %newUnseen) { next if $unseen{$_}; @@ -218,8 +214,10 @@ 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; + $log->dbug("About to speak/log"); + if ($hour >= 7) { + $log->dbug("Calling speak"); + speak $msg, $log; $log->msg($logmsg); } else { $log->msg("$logmsg [silent]"); @@ -228,16 +226,21 @@ sub MonitorMail() { $unseen{$_} = 1; } # for + # Let's time things + my $startTime = time; + # Re-establish callback + $log->dbug("Evaling idle"); eval { $IMAP->idle(\&MonitorMail) }; # If we return from idle then the server went away for some reason. With Gmail # the server seems to time out around 30-40 minutes. Here we simply reconnect # to the imap server and continue to MonitorMail. - $log->dbug("MonitorMail: Connection to $opts{imap} ended. Reconnecting"); + $log->dbug("MonitorMail: Connection to $opts{imap} ended - lasted " + . howlong $startTime); # Destroy current IMAP connection - $log->dbug("MonitorMail: Destorying IMAP connection to $opts{imap}"); + $log->dbug("MonitorMail: Destroying IMAP connection to $opts{imap}"); undef $IMAP; @@ -261,7 +264,7 @@ END { if ($log) { my $msg = "$FindBin::Script ending unexpectedly!"; - Say $msg; + speak $msg, $log; $log->err($msg); } # if @@ -282,6 +285,7 @@ GetOptions( 'usessl', 'useblocking', 'announce!', + 'append', ) || pod2usage; unless ($opts{password}) { @@ -297,15 +301,16 @@ if ($opts{username} =~ /.*\@(.*)$/) { if ($opts{daemon}) { # Perl complains if we reference $DB::OUT only once - my $foo = $DB::OUT; - EnterDaemonMode unless defined $DB::OUT; + no warnings; + EnterDaemonMode unless defined $DB::OUT or get_debug; + use warnings; } # if $log = Logger->new( - path => '/var/log', + path => '/var/local/log', name => "$Logger::me.$opts{name}", timestamped => 'yes', - append => 'yes', + append => $opts{append}, ); Connect2IMAP; @@ -318,7 +323,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);