Removed $tmpfile
[clearscm.git] / bin / announceEmail.pl
index 07cb6db..d21003b 100755 (executable)
@@ -31,23 +31,32 @@ $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 <username>] [-p|assword <password>]
-                         [-i|map <server]
+                         [-i|map <server>]
+                         [-an|nouce] [-ap|pend] [-da|emon] [-n|name <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)
+
+ Signals:
+   $SIG{USR1}:   Toggles debug option
+   $SIG{USR2}:   Reestablishes connection to IMAP server
 
 =head1 DESCRIPTION
 
@@ -93,6 +102,7 @@ my @greetings = (
   'I was looking in your inbox and found a message',
   'Not sure you want to hear this message',
   'Good news',
+  "What's this? A new message",
 );
 
 my %opts = (
@@ -104,9 +114,6 @@ my %opts = (
   username    => $ENV{USER},
   password    => $ENV{PASSWORD},
   imap        => $defaultIMAPServer,
-  usessl      => 0,
-  useblocking => 0,
-  announce    => 0,
 );
 
 sub interrupted {
@@ -121,7 +128,17 @@ sub interrupted {
   return;
 } # interrupted
 
+sub Connect2IMAP;
+
+sub restart {
+  $log->dbug("Re-establishing connection to $opts{imap} as $opts{username}");
+  Connect2IMAP;
+
+  goto MONITORMAIL;
+} # restart
+
 $SIG{USR1} = \&interrupted;
+$SIG{USR2} = \&restart;
 
 sub unseenMsgs() {
   $IMAP->select('inbox') or
@@ -133,6 +150,9 @@ sub unseenMsgs() {
 sub Connect2IMAP() {
   $log->dbug("Connecting to $opts{imap} as $opts{username}");
 
+  # Destroy any old connections
+  undef $IMAP;
+
   $IMAP = Mail::IMAPTalk->new(
     Server      => $opts{imap},
     Username    => $opts{username},
@@ -155,18 +175,21 @@ sub Connect2IMAP() {
 
 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{$_}) {
@@ -177,6 +200,7 @@ sub MonitorMail() {
     } # if
   } # for
 
+  $log->dbug("Processing new unseen messages");
   for (keys %newUnseen) {
     next if $unseen{$_};
 
@@ -208,7 +232,9 @@ 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.
+    $log->dbug("About to speak/log");
     if ($hour >= 7) {
+      $log->dbug("Calling speak");
       speak $msg, $log;
       $log->msg($logmsg);
     } else {
@@ -218,32 +244,19 @@ 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");
-
-  # Destroy current IMAP connection
-  $log->dbug("MonitorMail: Destorying IMAP connection to $opts{imap}");
+  restart;
 
-  undef $IMAP;
-
-  # Re-establish connection
-  Connect2IMAP;
-
-  $log->dbug("MonitorMail: Reconnected to IMAP server $opts{imap}");
-
-  # MonitorMail again - the dreaded goto! Seems the cleanest way to restart
-  # in this instance. I could call MonitorMail() recursively but that would
-  # leave junk on the stack.
-  $log->dbug('MonitorMail: Going back to the top of the loop');
-
-  goto MONITORMAIL;
-
-  return; # To make perlcritic happy
+  return;
 } # MonitorMail
 
 END {
@@ -272,6 +285,7 @@ GetOptions(
   'usessl',
   'useblocking',
   'announce!',
+  'append',
 ) || pod2usage;
 
 unless ($opts{password}) {
@@ -287,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/local/log',
   name        => "$Logger::me.$opts{name}",
   timestamped => 'yes',
-  append      => 'yes',
+  append      => $opts{append},
 );
 
 Connect2IMAP;